SLIDE 1
Fri., 16 Oct. 2015 Today: finish up cornucopia of types (subrange) - - PowerPoint PPT Presentation
Fri., 16 Oct. 2015 Today: finish up cornucopia of types (subrange) - - PowerPoint PPT Presentation
Fri., 16 Oct. 2015 Today: finish up cornucopia of types (subrange) Some more Haskell examples (to help with lab 6) For Monday read chapter 7.4-.5 (Arrays, strings) Composite Types (page 300) Subranges (see Program.adb in shared repo)
SLIDE 2
SLIDE 3
Lab 6 Help
Some Haskell examples (to help you with yesterday’s lab). Quick refresher (from “Learn You a Haskell…”): head [1,2,3,4] == 1 head “abcd” == ‘a’ tail [1,2,3,4] == [2,3,4] tail “abcd” == “bcd” 0 : [1,2,3,4] == [0,1,2,3,4] -- “:” is called the “cons” operator ‘z’ : “abcd” == “zabcd”
SLIDE 4
Concatenation vs. Cons
[1,2,3,4] ++ [5,6,7,8] == [1,2,3,4,5,6,7,8] -- concatenate “abcd” ++ “efgh” == “abcdefgh” Note the difference between : and ++. One of them inserts an element at the front of a list, the other joins together two
- lists. Thus,
[0] ++ [1,2,3] and 0 : [1,2,3] do the same thing. You can’t do this: [1,2,3]:4 but you can do this: [1,2,3,4]++[4]
SLIDE 5
List Comprehension
List comprehension is very, very useful! It is used to construct lists. General form: [ generic term using variables | domains of variables, other restrictions] A domain is an expression of the form x <- list, e.g., x <- [1..10] or y <- “abcde”. Other restrictions are predicates further limiting the variable values.
SLIDE 6
List Comprehension (continued)
Examples: [sqrt x | x <- [1..4]] [1.0,1.41421,1.73205,2.0] [a*b | a <- [2,3,4], b <- [3,5,7]] [6,10,14,9,15,21,12,20,28] [3 / x | x <- [-3..3], x /= 0] [-1.0,-1.5,-3.0,3.0,1.5,1.0]
SLIDE 7
Helper Functions
let f x y = head x : head y : [] f "abc" "def" "ad" f [1,2,3] [5,6,7] [1,5] let g x y = (f x y) ++ (f (tail x) (tail y)) g "abc" "def" "adbe"
SLIDE 8
Tuples (ordered pairs)
let a = (1,'b') fst a -- “first” 1 snd a -- “second” 'b' “zip” takes two lists of the same length and “zips them up” into a list of ordered pairs: zip "abc" "def" [('a','d'),('b','e'),('c','f')]
SLIDE 9