Warning

This page is located in archive.

types, definitions of functions, pattern matching, lists, basic built-in functions and operators: :load, :type (:t)

fact :: Integer -> Integer fact n = if n == 0 then 1 else n*(fact (n-1)) fact1 n | n==0 = 1 | otherwise = n*(fact1 (n-1)) fact'' 0 = 1 fact'' n = n*(fact'' (n-1))

data MyColor = Red | Green | Blue -- some sort of list of MyColor data MyListColor = CCons MyColor MyListColor | EmptyListColor data MyList a = Cons a (MyList a) | EmptyList

[1..100] -- list of numbers from 1 to 100 with step 1. [1,3..100] -- list of odd numbers from 1 to 100 (with step 2) [2,4..100] -- list of even numbers from 2 to 100 (with step 2) [1..] -- list of all numbers from 1 with step 1 [1,3..] -- list of all odd numbers from 1 (with step 2)

-- QuickSort in Haskell: quicksort [] = [] quicksort (e:ls) = (quicksort [x|x<-ls,x<e]) ++ [e] ++ (quicksort [x|x<-ls,x>=e])

lambda terms, curryfication, graphical syntax, @, let, where, type classes (class, instance, deriving), basic built-in type classes (Eq, Ord, Num, Show), :info (:i), lazy evaluation

-- Example of lambda term Main> map (\ x -> -x) [1..10] [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] -- Example of curryfication of - Main> map ((-) 0) [1..10] [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] -- Example of curryfication of infix - Main> map (0-) [1..10] [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] -- Example of let Main> let minus = (0-) in map minus [1..10] [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] -- Example of let Main> let {minus = (0-); ls = [1..10]} in map minus ls [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10] -- Example of where Main> map minus ls where {minus = (0-); ls = [1..10]} [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]

msort [] = [] msort [x] = [x] msort ls = merge sas sbs where (xs,ys) = split ls where split [] = ([],[]) split [x] = ([x],[]) split (x:y:ls) = ((x:xs),(y:ys)) where (xs,ys) = split ls sas = msort xs sbs = msort ys merge [] ls = ls merge ls [] = ls merge ws@(x:xs) zs@(y:ys) | (x<=y) = (x : merge xs zs) | otherwise = merge zs ws -- Example: Main> :t msort msort :: Ord a => [a] -> [a] Main> msort [6,4,1,3] [1,3,4,6]

-- automated instantiation of Eq, Ord, and Show class for NTree type using: deriving data NaryTree a = NaryNode a [NaryTree a] | NaryLeaf a deriving (Eq,Ord,Show) -- Example: Main> :t NaryNode 1 [NaryLeaf 2] NaryNode 1 [NaryLeaf 2] :: Num a => NaryTree a Main> NaryNode 2 [NaryLeaf 2] == NaryNode 1 [NaryLeaf 2] False Main> NaryNode 2 [NaryNode 2] <= NaryNode 1 [NaryLeaf 2] False Main> NaryNode 1 [NaryLeaf 2] NaryNode 1 [NaryLeaf 2]

data NaryTree a = NaryNode a [NaryTree a] | NaryLeaf a numberLeafs (NaryLeaf _) = 1 numberLeafs (NaryNode _ ls) = sum [numberLeafs x| x <- ls] -- custom instantiation of Eq, Ord, and Show class for NaryTree type using: instance instance (Eq a) => Eq (NaryTree a) where x == y = numberLeafs x == numberLeafs y instance (Ord a) => Ord (NaryTree a) where x <= y = numberLeafs x <= numberLeafs y instance (Show a) => Show (NaryTree a) where show (NaryLeaf x) = "<" ++ (show x) ++ ">" show (NaryNode x ls) = "{" ++ (show x) ++ ":" ++ (show ls) ++ "}" -- Example: Main> :t NaryNode 1 [NaryLeaf 2] NaryNode 1 [NaryLeaf 2] :: Num a => NaryTree a Main> NaryNode 2 [NaryLeaf 2] == NaryNode 1 [NaryLeaf 2] True Main> NaryNode 2 [NaryNode 2] <= NaryNode 1 [NaryLeaf 2]] True Main> NaryNode 1 [NaryLeaf 2] {1:[<2>]}

-- custom definition of some class W with one function w class W a where w :: a -> Bool -- instantiation of class W for type NTree a instance W (NaryTree a) where w (NaryLeaf _) = True w _ = False

courses/ae4b33flp/haskell.txt · Last modified: 2014/04/03 17:14 by vyskoji1