Headline

A unit of composition and separation of concerns

Illustration

Consider the following "naive" (inefficiently recursive) definition of the Fibonacci numbers in Language:Haskell:

-- Naive definition of Fibonacci numbers
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib x = fib (x-2) + fib (x-1)

For what it matters, we may place the function in a module as follows:

module Fibonacci.Inefficient where

-- The Fibonacci numbers
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib x = fib (x-2) + fib (x-1)

Suppose you want to compute the Fibonacci numbers more efficiently. To this end, you need an auxiliary function which keeps track of the two previous Fibonacci numbers so that binary (exponential) recursion can be avoided:

-- The Fibonacci numbers
fib :: Int -> Int
fib x = fib2 x 0 1

-- Helper function for efficient Fibonacci numbers
fib2 :: Int -> Int -> Int -> Int
fib2 0 y _ = y
fib2 1 _ y = y
fib2 x y1 y2 = fib2 (x-1) y2 (y1+y2)

These two functions are reasonably embedded into a module as follows:

module Fibonacci.Efficient (
  fib
) where

-- The Fibonacci numbers
fib :: Int -> Int
fib x = fib2 x 0 1

-- Helper function for efficient Fibonacci numbers
fib2 :: Int -> Int -> Int -> Int
fib2 0 y _ = y
fib2 1 _ y = y
fib2 x y1 y2 = fib2 (x-1) y2 (y1+y2)

Notably, the primary function for the Fibonacci numbers is exported while the helper function isn't. In this manner, we express that one function is of general interest whereas the other one is an implementational artifact.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.