Haskell a la carte
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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 eat?
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 supply arguments all around, so why clutter the notation with unnecessary ballast?