-- function tadd a b = a+b tmax a b = if a > b then a else b -- declaration tdefinition = "function can be used as constant with no argument, note that functionname always start with lower case letter" tconstant = "in ghci you can define function/constant/def using ' ghci> let ... = .... \ \ also it is possible to have multiline string this way " -- list tlistOfNumber = [1,2,3] tlistOfNumberConcat = tlistOfNumber ++ [4,5,6] tlistOfChar = ['a',' ','l','i','s','t',' ','o','f',' ','c','h','a','r'] tlistOfCharIsAString = tlistOfChar ++ " are string, it work with all list function" -- cons tcons = 'a':" consed list" tcons2 = 'a':'n':"other consed list" tTailOfString = head tString tHeadOfString = tail tString tInitOfString = init tString tfourFirst = take 4 trange -- string tString = "a string" tFourthLetterOfString = tString !! 3 -- generator trange = [1..10] trangeByTwo = [2,4..10] trangeInfinite = [1..] trepeat = repeat 't' tcycle = cycle ['/', '-', '\\', '-'] treplicate3time = replicate 3 "time" tlistcomprehension = [ x * 5 | x <- [1..] , x < 10 ] -- tuple ttreple a b c = (a,b,c) ttuple a b = (a,b) ttupleA tpl = fst tpl ttupleB tpl = snd tpl tzip = zip [1..3] [3..6] -- |how you document and type a function properly -- |this will be added to function definition -- this will not tproc :: String tproc = "A String is returned" {-| multiline comment FTW -} tmonum :: Int -> Int tmonum a = a -- |return factiorial of n tfact :: Int -> Int tfact n = product [1..n] -- |reuse long type name ttoint :: (Num n) => n -> Int ttoint n = 1 -- |pattern tpat :: String -> Bool tpat "Moe" = True tpat "Homer" = True tpat m = False -- |guard tgui :: String -> Bool tgui name | ((take 3 name)) == "gui" = True | otherwise = False -- |guard with where clause tgui2 :: String -> Bool tgui2 name | threeFirst == lower = True | threeFirst == startUpperCase = True | threeFirst == allUpperCase = True | otherwise = False where threeFirst = take 3 name lower = "gui" startUpperCase = "Gui" allUpperCase = "GUI" -- |where clause is not exclusive to guard tinitial firstname lastname = [f] ++ ", " ++ [l] ++ "." where (f:_) = firstname (l:_) = lastname -- |let expression are like where but is only accessible from the inner code tinitial2 firstname lastname = let f = head firstname l = head lastname in [f] ++ ", " ++ [l] ++ "." -- im a declaration tten = (let first = 1 ; last = 10 in [first..last]) -- |replace guard with let expression tgui3 name = let tgui3' "gui" = True tgui3' "Gui" = True tgui3' "gui" = True tgui3' name' = False in (tgui3' (take 3 name)) -- |There is also case expression tlstabout lst = "the list is " ++ case lst of [] -> "empty" [_] -> "singleton" as -> "gluton" -- |quicksort tquicksort :: (Ord a) => [a] -> [a] tquicksort [] = [] tquicksort (pivot:rest) = let biggest = tquicksort [elem | elem <- rest, elem >= pivot] smallest = tquicksort [elem | elem <- rest, elem < pivot] in biggest ++ [pivot] ++ smallest -- |let as code place ttriangler a b c = let h = a^2 + b^2 yes = c^2 ==h in yes