Basic Haskell: lists and list notations

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Although Haskell provides for convenient definition of data types and structures, lists are pre-defined and ubiquitous (storage is automatically allocated and collected)

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Lists can be written easily using square brackets and commas as delimiters ```> reverse [1, 2, 3, 4, 5] [5,4,3,2,1]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null In fact, lists are defined as a recursive data type (as in LISP), constructed from the head (first item) and tail ```> head [1,2,3,4] 1 > tail [1,2,3,4] [2,3,4] > 1 : [2,3,4] [1,2,3,4] >``` (LISPs "cons" is written with an infix colon)

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains As a convenient further short-hand, lists over ordered domains can be written with a special ellipsis notation ```> [1..10] [1,2,3,4,5,6,7,8,9,10] > [0,2..20] [0,2,4,6,8,10,12,14,16,18,20]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains Strings as lists of characters In fact, Haskell's strings are just lists over the atomic Char type (by default, they are printed using the quoted form) ```> :t "hello" "hello" :: String > ['h', 'e', 'l', 'l', 'o'] "hello" > ['a'..'z'] "abcdefghijklmnopqrstuvwxyz"```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains Strings as lists of characters Component and lists types Haskell lists are polymorphic but homogeneous: different lists may hold different types of elements, but a single list must hold just one uniform type of element ```> [True, False, True] [True,False,True] > :t \$\$ [True,False,True] :: [Bool] > :t reverse reverse :: [a] -> [a]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains Strings as lists of characters Component and lists types Convenient list functions Many useful functions on lists are provided in a standard Prelude ```> length ['a'..'z'] 26 > [1..10] ++ reverse [1..10] [1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,5,4,3,2,1]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains Strings as lists of characters Component and lists types Convenient list functions Infinite lists and lazy evaluation Haskell provides lazy evaluation for functions and data structures, so we can define infinite lists (it is prudent to avoid evaluation of a whole infinite list!) ```> [1,3..] [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,{Interrupted!} > take 20 [1,3..] [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39]```

```

```

 3rd CCSC Northwest Conference • Fall 2001

Basic Haskell: lists and list notations
 Lists in Haskell Bracket notation Infix notation and head, tail, null Ellipses for ordered domains Strings as lists of characters Component and lists types Convenient list functions Infinite lists and lazy evaluation Z-F expressions One last notational convenience (due to David Turner) is list comprehension, which mimics notation from Zermelo-Fraenkel set theory ```> [ a * b | a<-[1..3], b<-reverse [1..4] ] [4,3,2,1,8,6,4,2,12,9,6,3] > [ 2^i | i<-[1..10], odd i ] [2,8,32,128,512]```

```

```