Here are few examples written in Haskell to show off the capabilities and elegance of the language. Please note that the type definitions in the first lines are optional.
the factorial functionfac :: Integer -> Integer fac 0 = 1 fac n | n > 0 = n * fac (n-1)the quicksort algorithm
qsort :: (Ord a) => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort [y|y<-xs,y<x] ++[x]++ qsort [y|y<-xs,y>=x]binary tree
data BTree a = NilTree | Node a (BTree a) (BTree a)some other examples ...
-- program to generate C code for a statechart -- Transition = condition + action + newState data Transition = TRANSITION String String String -- a StateChart contains an id, a list of transitions and a list of sub-states data StateChart = STATE String [Transition] [StateChart] -- convert a state p :: String -> StateChart -> [String] p parent (STATE id transitions subStates) = makeCase parent id transitions : concat [p id x | x <- subStates] -- make/format a "C" case makeCase :: String -> String -> [Transition] -> String makeCase parent id transitions = " case " ++ id ++ ":\n" ++ id ++ ":\n" ++ concat [makeTr x | x <- transitions] ++ " goto " ++ parent ++ ";\n\n" -- print/format a transition entry makeTr :: Transition -> String makeTr (TRANSITION cond act newState) = " if (" ++ cond ++ ") {" ++ act ++ "; state=" ++ newState ++ "; return}\n" -------------------------------------------------------- -- create an example state-chart t = STATE "root" [ TRANSITION "cond()" "action()" "c", TRANSITION "c2()" "a++" "e" ] [ STATE "b" [TRANSITION "x==2" "proc()" "c"] [], STATE "c" [] [], STATE "d" [] [ STATE "e" [TRANSITION "x==0" "x=2*y" "d"] [] ] ] -------------------------------------------------------- s = p "nil" t -- create list of C structures ss = " switch (state) {\n" ++ foldr1 (++) s ++ " }\n" -- combine them into one string main = putStr ss