## Concept:

# Module

## 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

- Vocabulary:Programming
- Vocabulary:Software architecture
- Abstraction mechanism
- Modularization
- Modular_programming
- http://en.wikipedia.org/wiki/Modular_programming
- http://www.haskell.org/tutorial/modules.html

### There are no revisions for this page.

## User contributions

## 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.
**

#### 10 most similar pages:

- Technology:QuickCheck - 0.6933177
- Concept:Type class - 0.69089776
- Concept:Lazy evaluation - 0.6418231
- Concept:Merge sort - 0.5626931
- Concept:Insertion sort - 0.5468288
- Concept:Rose tree - 0.5404761
- Concept:Sum monoid - 0.4922064
- Concept:Greatest common divisor - 0.49156606
- Concept:Factorial - 0.4847458
- Concept:Semantic equality - 0.44062155

## 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.
**