# Haskell a la carte

### From HaskellWiki

(Difference between revisions)

(→Potages) |
(→Entrées) |
||

Line 38: | Line 38: | ||

* | * | ||

<haskell> | <haskell> | ||

− | square :: | + | square :: Int -> Int |

square x = x*x | square x = x*x | ||

</haskell> | </haskell> | ||

Line 68: | Line 68: | ||

</haskell> | </haskell> | ||

::<math>a^n</math>, defined with ''recursion''. Assumes that the exponent <hask>n</hask> is not negative, that is <hask>n >= 0</hask>. | ::<math>a^n</math>, defined with ''recursion''. Assumes that the exponent <hask>n</hask> is not negative, that is <hask>n >= 0</hask>. | ||

− | :: Recursion is the basic building block for iteration in Haskell, there are no <code>for</code> or <code>while</code>-loops. Well, there are | + | :: Recursion is the basic building block for iteration in Haskell, there are no <code>for</code> or <code>while</code>-loops. Well, there are functions like <hask>map</hask> or <hask>foldr</hask> that provide something similar. There is no need for special built-in control structures, you can define them yourself as ordinary functions (later). |

* | * | ||

Line 76: | Line 76: | ||

</haskell> | </haskell> | ||

::Exponentiation again, this time with ''pattern matching''. The first equation that matches will be chosen. | ::Exponentiation again, this time with ''pattern matching''. The first equation that matches will be chosen. | ||

+ | |||

+ | * | ||

+ | <haskell> | ||

+ | length [] = 0 | ||

+ | length (x:xs) = 1 + length xs | ||

+ | </haskell> | ||

+ | ::Calculate the length of a ''list''. What's a list? Well, a list may either be empty (<hask>[]</hask>) or be an element (<hask>x</hask>) prepended (<hask>:</hask>) to another list (<hask>xs</hask>). Read "<hask>xs</hask>" as the plural of "<hask>x</hask>", that is as "ex-es". It's a list of other such elements <hask>x</hask>, after all. | ||

+ | |||

+ | * | ||

+ | <haskell> | ||

+ | length :: [a] -> Int | ||

+ | length [] = 0 | ||

+ | length (x:xs) = 1 + length xs | ||

+ | </haskell> | ||

+ | ::Length of a list again, this time with type signature. What type must the elements be of? It doesn't matter, length will work for any such type <hask>a</hask>. | ||

+ | |||

+ | * | ||

+ | <haskell> | ||

+ | sum [] = 0 | ||

+ | sum (x:xs) = x + sum xs | ||

+ | </haskell> | ||

+ | ::Sum all elements in a list. Similar to the previous one. | ||

+ | |||

+ | * | ||

+ | <haskell> | ||

+ | average xs = sum xs / (fromIntegral (length xs)) | ||

+ | </haskell> | ||

+ | ::Arithmetic mean. <hask>fromIntegral</hask> converts the integer result of <hask>length</hask> into a decimal number for the division <hask>/</hask>. | ||

== Soupes == | == Soupes == |

## Revision as of 16:49, 14 December 2007

New to Haskell? This menu will give you a first impression. Don't read all the explanations, or you'll be starved before the meal.

## Contents |

## 1 Apéritifs

Foretaste of an excellent meal.

qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (>=x) xs))

- Quicksort in three lines (!). Sorts not only integers but anything that can be compared.

fibs = 1:1:zipWith (+) fibs (tail fibs)

- The
*infinite*list of fibonacci numbers. Just don't try to print all of it.

- The

linecount = interact $ show . length . lines wordcount = interact $ show . length . words

- Count the number of lines or words from standard input.

## 2 Entrées

How to read the dishes.

square x = x*x

- is the function which maps a number to its square. While we commonly write parenthesis around function arguments in mathematics and most programming languages, a simple space is enough in Haskell. We're going to apply functions to arguments all around, so why clutter the notation with unnecessary ballast?

square :: Int -> Int square x = x*x

- Squaring again, this time with a
*type signature*which says that squaring maps integers to integers. In mathematics, we'd write . Every expression in Haskell has a type and the compiler will automatically infer (= figure out) one for you if you're too lazy to write down a type signature yourself. Of course, parenthesis are allowed for grouping, like inwhich is 36 compared tosquare (4+2)which is 16+2=18.square 4 + 2

- Squaring again, this time with a

square :: Num a => a -> a square x = x*x

- Squaring yet again, this time with a more general type signature. After all, we can square anything () that looks like a number (a). By the way, this general type is the one that the compiler will infer forNum aif you omit an explicit signature.square

- Squaring yet again, this time with a more general type signature. After all, we can square anything (

average x y = (x+y)/2

- The average of two numbers. Multiple arguments are separated by spaces.

average :: Double -> Double -> Double average x y = (x+y)/2

- Average again, this time with a type signature. Looks a bit strange, but that's the spicey
*currying*. In fact,is a function that takes only one argument (average) but returns a function with one argument (Double).Double -> Double

- Average again, this time with a type signature. Looks a bit strange, but that's the spicey

power a n = if n == 0 then 1 else a * power a (n-1)

*a*^{n}, defined with*recursion*. Assumes that the exponentis not negative, that isn.n >= 0- Recursion is the basic building block for iteration in Haskell, there are no
`for`

or`while`

-loops. Well, there are functions likeormapthat provide something similar. There is no need for special built-in control structures, you can define them yourself as ordinary functions (later).foldr

power a 0 = 1 power a n = a * power a (n-1)

- Exponentiation again, this time with
*pattern matching*. The first equation that matches will be chosen.

- Exponentiation again, this time with

length [] = 0 length (x:xs) = 1 + length xs

- Calculate the length of a
*list*. What's a list? Well, a list may either be empty () or be an element ([]) prepended (x) to another list (:). Read "xs" as the plural of "xs", that is as "ex-es". It's a list of other such elementsx, after all.x

- Calculate the length of a

length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs

- Length of a list again, this time with type signature. What type must the elements be of? It doesn't matter, length will work for any such type .a

- Length of a list again, this time with type signature. What type must the elements be of? It doesn't matter, length will work for any such type

sum [] = 0 sum (x:xs) = x + sum xs

- Sum all elements in a list. Similar to the previous one.

average xs = sum xs / (fromIntegral (length xs))

- Arithmetic mean. converts the integer result offromIntegralinto a decimal number for the divisionlength./

- Arithmetic mean.

## 3 Soupes

The best soup is made by combining the available ingredients.

(.) :: (b -> c) -> (a -> b) -> (a -> c) (.) f g x = f (g x) fourthPower = square . square

- The dot is good old function composition . First apply g, then apply f. Use it for squaring something twice.f . g

- The dot