Haskell
From Ggl's wiki
Contents |
What is Haskell
Haskell is a pure functional programming language. Its main features are:
- purity (no side-effects)
- high-order functions
- lazy evaluation: a expression is evaluated only when needed
- strongly typed with type inference
- polymorphism
- currying
- pattern matching
- list comprehension
- monads
- type classes
- layout parsing: code blocks can be delimited by the layout (tab spaces)
Functions
composition: rotate = flipV . flipH forward composition: rotate = flipH >.> flipV
lambda: \x y = y*y
partial application:
sum x y = x + y sum 1 -> 1 + y
Lists
(head:tail) commonly written (x:xs)
List comprehension: [y | y <- xs, y <= x]
Useful functions: map, filter, foldr1, foldr, zip, zipWith
Examples
Some examples could be find there.
module Main where import System.IO import Char
Define some custom types. Simple types:
type Person = String type Book = String
List [] of (,) tuples (here pairs):
type LoanDB = [(Person, Book)]
Define the function that build the exemple database list:
exampleDB :: LoanDB
exampleDB = [("Alice", "Tintin"),
("Anna", "Little Women"),
("Alice", "Asterix"),
("Rory", "Tintin")]
Retrieve books from LoadDB db that were loaned by Person borrower. It uses a list comprehension where a book is in the returned list, if the person in the tuple (person, book) from db is the borrower:
books :: LoanDB -> Person -> [Book]
books db borrower
= [ book | (person, book) <- db, person==borrower]
Prepend the tuple (borrower, book) to db. ++ is the concatenation operator.
makeLoan :: LoanDB -> Person -> Book -> LoanDB makeLoan db borrower book = [(borrower, book)] ++ db
Remove (borrower, book) from db. Actually, it uses a list comprehension to return a list where all but (borrower, book) tuples are.
returnLoan :: LoanDB -> Person -> Book -> LoanDB returnLoan db borrower book = [(bwer, bk) | (bwer, bk) <- db, (bwer, bk) /= (borrower, book)]
Return a list of people who borrowed book:
borrowers :: LoanDB -> Book -> [Person]
borrowers db book
= [borrower | (borrower, bk) <- db, bk==book]
Tell if book was borrowed. Return True is there is at least one borrower e.g. length(borrowers db book) > 0.
borrowed :: LoanDB -> Book -> Bool
borrowed db book
= if length(borrowers db book) > 0
then True
else False
How many books did borrower borrow:
numBorrowed :: LoanDB -> Person -> Int
numBorrowed db borrower
= length(books db borrower)
In a list (x:xs), x is the head and xs the tail. It's often used in list recursion.
Pattern matching:
sum :: [Int] -> Int sum [] = 0 sum (x:xs) = x + sum xs
firstDigit st
= case (digits st) of
[] ->'\O'
(x:_) -> X
Guards:
comparison x y
| x < y = "The first is less"
| x > y = "The second is less"
| otherwise = "They are equal"
Insertion in an ordered list:
ins :: Int -> [Int] -> [Int] ins x [] = [x] ins x (y:ys) = | x <= y = x:(y:ys) | otherwise = y : ins x ys
Quicksort:
qSort :: [Int] -> [Int] qSort [] = [] qSort (x:xs) = qSort [y | y <- xs, y <= x] ++ [x] ++ qSort [y | y <- xs, y > x]

