1 / 7

isort :: [Int] -> [Int]

isort :: [Int] -> [Int]. isort [ ] = [ ] isort (head : tail) = insert head (isort tail)     where insert v [] = [v] insert v (h : t) = if  v <= h then v : h : t else h : (insert v t) Main> isort [9,5,2,8,3] [2,3,5,8,9]. List Comprehensions. General form Body Selector

freira
Télécharger la présentation

isort :: [Int] -> [Int]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. isort :: [Int] -> [Int] isort [] = [] isort (head : tail) = insert head (isort tail)     where insert v [] = [v] insert v (h : t) = if  v <= h then v : h : t else h : (insert v t) Main> isort [9,5,2,8,3] [2,3,5,8,9] Lecture 8 List Comprehensions

  2. List Comprehensions Lecture 8 List Comprehensions

  3. General form BodySelector [ expression | qualifiers] squair list = [ x*x | x <-list ] generator (x is an element of list) Main> squair [1,2,3,4,5] [1,4,9,16,25] The selector is a list expression. It can be combined with one or more guards, separated by commas. A qualifier can be a generator or a filter(a boolean expression restricting what is generated by the generator. Prelude>[x | x<- [1..10], x `mod` 4 = = 0] [4,8] Lecture 8 List Comprehensions

  4. Example: Generating English Sentences stat=[x++" "++y++" "++z++".“| x<-["A","An","The"], y<-["man","ape","dog"], z<-["runs","flies","ate"], not(x == "An" && not(member (head y) "aeiou")|| x=="A" && member (head y) "aeiou")] ["A man runs.","A man flies.","A man ate.","A dog runs.","A dog flies.","A dog ate.","An ape runs.","An ape flies.","An ape ate.","The man runs.","The man flies.","The man ate.","The ape runs.","The ape flies.","The ape ate.","The dog runs.","The dog flies.","The dog ate."] Lecture 8 List Comprehensions

  5. avgCouples :: [(Float,Float)] -> [Float] avgCouplelist = [ (a+b)/2 | (a,b) <-list ] Prelude > avgCouples [(2.3,3.7),(4.5,5.5),(5.6,8.4)] [3.0,5.0,7.0] Prelude> take 9 [(a,b,c)|a<-[1..],b<-[1..],c<-[1..],a+b+c<1000] [(1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,1,5),(1,1,6),(1,1,7),(1,1,8),(1,1,9)] Prelude> [ (a,b) | a <- [1,2], b <- [3,4,5] ] [(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)] Prelude > [(a,b)|a <-[1,2,3,4,5,6,7],b<-[7,8,9,10,11,12],even a,odd b] [(2,7),(2,9),(2,11),(4,7),(4,9),(4,11),(6,7),(6,9),(6,11)] Prelude > [ (a,b) | a <- ["A","The"], b <- ["dog","cat","bird"] ] ("A","dog"),("A","cat"),("A","bird"),("The","dog"),("The","cat"),("The","bird") Lecture 8 List Comprehensions

  6. Function oddL oddL [ ] = [ ] oddL list = if even (headlist) then oddL (taillist) else headlist:oddL (taillist) oddL [ ] = [ ] oddL (h:t) = if evenh then oddL t else h:oddL t Function oddL using list comprehension OddL list = [x | x <- list, x `mod` 2 / = 0] Lecture 8 List Comprehensions

  7. Quicksort qsort [] = [] qsort (head:tail) = qsort [x | x <- tail, x < head] ++ [head] ++ qsort [x | x <- tail, x>=head] Main> qsort [6,2,1,8,4,9] [1,2,4,6,8,9] Main> qsort ['c','b','a','s','d'] "abcds" Main> qsort["George","Paul","Andy","Martin","Abraham","Mary","Stewart"] ["Abraham","Andy","George","Martin","Mary","Paul","Stewart"] Main> qsort [[8,9,4],[1,3,5],[9,7,0],[6,9,2]] [[1,3,5],[6,9,2],[8,9,4],[9,7,0]] Lecture 8 List Comprehensions

More Related