



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Lists are the bread and butter of functional programming. At least they started out that way. The first functional programming language, LISP - List Process- ing, 1960 by John McCarthy, had list as the fundamental data-structure. LISP is great at symbolic processing - the AI programming language. Using the Haskell notation we can write list as data Nat [a] = [] | a:[a] so we can say
Hugs> :t [] [] :: [a] Hugs> :t (:) (:) :: a -> [a] -> [a]
The term cons actually means list constructor.
null2 [] = T rue null 2 = F alse
” ” is a specification that matches any pattern Alternative: If we define null as
null 3 x = (x == [])
then the type is
Main> :t null null1 :: Eq [a] => [a] -> Bool
if (x==[]) then True else False is a really dumb way of defining the above function. if b then e1 else e2, where
Evaluation for if-then-else if True then e1 else e2 ; e 1 if False then e1 else e2 ; e 2 In the book null is defined as:
null2 [] = T rue null2 (x : xs) = F alse
How is this different from one above?
null 1 ⊥ = ⊥
So- null is strict - i.e. if it is applied to ⊥ then the result is ⊥. null[(+), (−)]
:t [(+),(-)] [(+),(-)] :: Num a => [a -> a -> a] Main>
But we cannot do so for null3, it generates a type error.
Main> null3 [(+),(-)] ERROR - Unresolved overloading *** Type : (Num a, Eq (a -> a -> a)) => Bool *** Expression : null3 [(+),(-)]
But, null2 and null1 behave differently than null3.
Main> null2 [(+),(-)] False Main> null1 [(+),(-)] False
So null1 and null2 apply to a wider class of list types - those that are instances of the Eq type class and those that are not.
Let’s do concat
Main> :t concat concat :: [[a]] -> [a]
concat [[1,2],[],[3,4,5]] = [1,2,3,4,5] and is defined as concat [] = [] concat (x : xs) = x + +(concat xs) How do we compute with the concat function? concat[[1, 2 , 3]] = [1, 2 , 3] + +concat [] = [1, 2 , 3] + +[] = [1, 2 , 3]
Another example: concat[[1, 2 , 3], [], [4, 5]] = [1, 2 , 3] + +concat [[], [4, 5]] = [1, 2 , 3] + +[] + +concat [[4, 5]] = [1, 2 , 3] + +([] + +[4, 5] + +concat []) = [1, 2 , 3] + +([] + +([4, 5] + +[])) =... = [1, 2 , 3 , 4 , 5]
Main> :t zip zip :: [a] -> [b] -> [(a,b)]
We define it by recursion on the first argument. Here’s an alternate definition zip []ys = [] zip (x : xs) ys = (x, headys) : zip xs (tailys) zip (x : xs) (y : ys) = (x, y) : zip xs ys Design choice for zip - what is the right length ??
length (zip xs ys) ? = min (length xs, length ys) ? = max(length xs, length ys) ? = if length xs <> length ys then error else length xs ? = length xs or if length xs > length ys then error
zip1 is strict in both argument
zip1 [] [] = [] zip1 (x : xs) [] = [] zip1 [] (x : xs) = [] zip1 (x : xs)(y : ys) = (x, y) : zip 1 xsys
zip′^ [] ⊥ = [] zip′^ ⊥ [] = ⊥ zip [] ⊥ = ⊥
zip’ is not strict in its second argument.
zip [] ys = [] zip xs [] = [] zip (x : xs) (y : ys) = (x, y) : zip xs ys
In Haskll there is intersting notation
last [] = error last (x : xs) = if (null xs) then x else last xs
last [1, 2 , 3] = last [2, 3] = last [3] = 3