Docsity

Lecture Notes on Functions on Lists - Functional Programming | COSC 3015, Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 9
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
September 23, 2008
1 Functions on lists
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]
1.1 null function
The term cons actually means list constructor.
null2 [] = Tr ue
null2=F alse
” ” is a specification that matches any pattern
Alternative:
If we define null as
null3x= (x== [])
then the type is
Main> :t null
null1 :: Eq [a] => [a] -> Bool
1
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Functions on Lists - Functional Programming | COSC 3015 and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 9

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

September 23, 2008

1 Functions on lists

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]

1.1 null function

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

  • b - bool
  • e 1 , e2 - both must have the same type.

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.

1.5 concat

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]

1.6 zip

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

  • An infinits list of ints: [1..] = [1, 2 , 3 , 4 ,.. .]
  • Partial lists
    1. ⊥
    2. 1 : ⊥
    3. 1 : 2 : ⊥
  • Finite lists [⊥, ⊥] - the type of lists is [a]

1.7 last

last [] = error last (x : xs) = if (null xs) then x else last xs

last [1, 2 , 3] = last [2, 3] = last [3] = 3