هسکل (۳)
اسلاید 1: Lecture 9 List Comprehensions1isort :: [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]
اسلاید 2: Lecture 9 List Comprehensions2List Comprehensions
اسلاید 3: Lecture 9 List Comprehensions3generator (x is an element of list)Main> squair [1,2,3,4,5] [1,4,9,16,25]squair list = [ x*x | x <- list ] General form Body Selector[ expression | qualifiers]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]
اسلاید 4: Lecture 9 List Comprehensions4stat = [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.]Example: Generating English Sentences
اسلاید 5: Lecture 9 List Comprehensions5avgCouple list = [ (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)avgCouples :: [(Float,Float)] -> [Float]
اسلاید 6: Lecture 9 List Comprehensions6oddL [ ] = [ ]oddL list = if even (head list) then oddL (tail list) else head list:oddL (tail list)oddL [ ] = [ ]oddL (h:t) = if even h then oddL t else h:oddL tFunction oddL using list comprehensionOddL list = [x | x <- list, x `mod` 2 / = 0]Function oddL
اسلاید 7: Lecture 9 List Comprehensions7qsort [] = []qsort (head:tail) = qsort [x | x <- tail, x < head]++ [head]++ qsort [x | x <- tail, x>=head]QuicksortMain> qsort [6,2,1,8,4,9][1,2,4,6,8,9]Main> qsort [c,b,a,s,d]abcdsMain> 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]]
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.