Haskell a la carte

From HaskellWiki
Revision as of 17:22, 14 December 2007 by Apfelmus (talk | contribs) (Desserts)

Jump to: navigation, search



Foretaste of an excellent meal.

  qsort []     = []
  linecount = interact $ show . length . lines
  wordcount = interact $ show . length . words
Count the number of lines or words from standard input.


How to read the dishes.

  square x = x*x
  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 f:\mathbb{Z}\to\mathbb{Z},\ f(x)=x\cdot x. 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
  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).
a^n, defined with recursion. Assumes that the exponent n is not negative, that is n >= 0.
Recursion is the basic building block for iteration in Haskell, there are no for or while-loops. Well, there are functions like map or foldr that provide something similar. There is no need for special built-in control structures, you can define them yourself as ordinary functions (later).
  power a 0 = 1
  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 (x) prepended (:) to another list (xs). Read "xs" as the plural of "x", that is as "ex-es". It's a list of other such elements x, after all.
  length :: [a] -> Int
  length []     = 0
  length (x:xs) = 1 + length xs
Length of a list again, this time with type signature. [a] is the type of lists with elements of type a. length can be used for any such element type.
  head :: [a] -> a
  head (x:xs) = x
First element of a list. Undefined for empty lists.
  sum []     = 0
  sum (x:xs) = x + sum xs
Sum all elements of a list.
Arithmetic mean. fromIntegral converts the integer result of length into a decimal number for the division /.


The best soup is made by combining the available ingredients.

  (.) f g x = f (g x)

  fourthPower = square . square
The dot f . g is good old function composition f \circ g. First apply g, then apply f. Simple example: squaring something twice.
  minimum = head . qsort

Plats principaux


Sugar-sweet and en passant.