## 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.6943546
- Concept:Type class - 0.6892892
- Concept:Lazy evaluation - 0.6406563
- Concept:Merge sort - 0.5609417
- Concept:Insertion sort - 0.5454851
- Concept:Rose tree - 0.54461586
- Concept:Sum monoid - 0.49169546
- Concept:Greatest common divisor - 0.4885628
- Concept:Factorial - 0.48343235
- Concept:Fold algebra - 0.440448

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