Difference between revisions of "Haskell a la carte"

From HaskellWiki
Jump to navigation Jump to search
 
Line 1: Line 1:
[[Categories:Tutorials]]
+
[[Category:Tutorials]]
  +
  +
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.
   
 
== Apéritifs ==
 
== Apéritifs ==
Line 8: Line 10:
 
qsort :: Ord a => [a] -> [a]
 
qsort :: Ord a => [a] -> [a]
 
qsort [] = []
 
qsort [] = []
qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (>x) xs))
+
qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (>=x) xs))
 
</haskell>
 
</haskell>
 
::Quicksort in three lines (!). Sorts not only integers but anything that can be compared.
 
::Quicksort in three lines (!). Sorts not only integers but anything that can be compared.
Line 26: Line 28:
   
 
== Entrées ==
 
== Entrées ==
How to eat?
+
How to start eating?
   
 
*
 
*
Line 32: Line 34:
 
square x = x*x
 
square x = x*x
 
</haskell>
 
</haskell>
::The function <math>f(x)=x\cdot x</math> 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 supply arguments all around, so why clutter the notation with unnecessary ballast?
+
::The function <math>f(x)=x\cdot x</math> 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?
  +
  +
*
  +
<haskell>
  +
square :: Integer -> Integer
  +
square x = x*x
  +
</haskell>
  +
:: Squaring again, this time with a ''type signature'' which says that squaring maps integers to integers. In mathematics, we'd write <math>f:\mathbb{Z}\to\mathbb{Z},\ f(x)=x\cdot x</math>. 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 in <hask>square (4+2)</hask> which is 36 compared to <hask>square 4 + 2</hask> which is 16+2=18.
  +
  +
*
  +
<haskell>
  +
square :: Num a => a -> a
  +
square x = x*x
  +
</haskell>
  +
:: Squaring yet again, this time with a more general type signature. After all, we can square anything (<hask>a</hask>) that looks like a number (<hask>Num a</hask>). By the way, this general type is the one that the compiler will infer for <hask>square</hask> if you omit an explicit signature.
  +
  +
*
  +
<haskell>
  +
average x y = (x+y)/2
  +
</haskell>
  +
:: The average of two numbers. Multiple arguments are separated by spaces.
  +
  +
*
  +
<haskell>
  +
average :: Double -> Double -> Double
  +
average x y = (x+y)/2
  +
</haskell>
  +
::Average again, this time with a type signature. Looks a bit strange, but that's the spicey ''currying''. In fact, <hask>average</hask> is a function that takes only one argument (<hask>Double</hask>) but returns a function with one argument (<hask>Double -> Double</hask>).
  +
  +
== Potages ==
  +
The best soup is made by combining well-known ingredients.
  +
  +
*
  +
<haskell>
  +
(.) :: (b -> c) -> (a -> b) -> (a -> c)
  +
(.) f g x = f (g x)
  +
  +
fourthPower = square . square
  +
</haskell>
  +
::The dot <hask>f . g</hask> is good old function composition <math>f \circ g</math>: first apply g, then apply f. Use it for squaring something twice.
  +
   
 
== Plats principaux ==
 
== Plats principaux ==

Revision as of 13:44, 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.

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.
  linecount = interact $ show . length . lines
  wordcount = interact $ show . length . words
Count the number of lines or words from standard input.

Entrées

How to start eating?

  square x = x*x
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 :: Integer -> Integer
  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 in square (4+2) which is 36 compared to square 4 + 2 which is 16+2=18.
  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 (a) that looks like a number (Num a). By the way, this general type is the one that the compiler will infer for square if you omit an explicit signature.
  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, average is a function that takes only one argument (Double) but returns a function with one argument (Double -> Double).

Potages

The best soup is made by combining well-known ingredients.

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

  fourthPower = square . square
The dot f . g is good old function composition : first apply g, then apply f. Use it for squaring something twice.


Plats principaux

Desserts

Vins