fri 16 oct 2015
play

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)


  1. 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)

  2. Composite Types (page 300) Subranges (see ‘Program.adb’ in shared repo) Nothing quite like it in C or Java. Lets you put bounds on the allowed values for certain variables. E.g., if we really want an integer variable to hold only values between 1 and 31, we can create a subrange type. (Pascal and Ada both have this.) Example (Ada): subtype Day is Integer range 1..31; ... d: Day; -- d may hold only values 1,...,31

  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”

  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]

  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.

  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]

  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"

  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')]

  9. More Examples See Oct 16 repository for examples “exchange” and “franz” (worked in class)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend