



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




More generally, for any functionmultAll m lst = [ mx | x <- lst]multAll :: Int -> [Int] -> [Int]addPairs pairList = [[m+n] | (m,n) <- pairList]addPairs :: [(Int,Int)] -> [[Int]]doubleAll lst = [ 2x | x <- lst] doubleAll :: [Int] -> [Int] Applying a transformation to every element of a list:
(^) f (^) of type (^) a -> b , we can define a
function (^) applyF (^) with type (^) [a] -> [b]
(^) by:
applyF lst = [
(^) f x | x <- lst]
More generally, for any functionmultAll m (x:xs) = mx : multAll m xsmultAll m [] = []addPairs ((m,n):rest) = [m+n] : addPairs restaddPairs [] = []doubleAll (x:xs) = 2x : doubleAll xs doubleAll [] = []
(^) f (^) of type (^) a -> b , we can define a
function (^) applyF (^) with type (^) [a] -> [b]
(^) by:
applyF (x:xs) = applyF [] = []
(^) f (^) x : (^) applyF xs
For any property expressed as functionhalfPairs pairList = [(m,n) | (m,n) <- pairList, n == 2*m]halfPairs :: [(Int,Int)] -> [(Int,Int)]lessThanTen lst = [ x | x <- lst, x < 10] lessThanTen :: [Int] -> [Int] Filtering elements from a list:
(^) p (^) of type (^) a -> Bool
, we can
define a function
(^) filterP (^) with type (^) [a] -> [a]
(^) by:
filterP lst = [ x | x <- lst,
(^) p x]
filter p lst = [ x | x <- lst, p x] filter :: (a -> Bool) -> [a] -> [a]^ To be even more generic:
isHalfPair (m,n) = isHalfPair :: (Int,Int) -> Bool^ Examples:filter p lst = [ x | x <- lst, p x] filter :: (a -> Bool) -> [a] -> [a]^ Recall:
(^) (n == 2*m)
filter isDigitfilter isHalfPair [(3,7),(4,8),(5,9),(10,21),(11,22)]
(^) [โaโ, โ3โ, โ7โ, โ\nโ, โbโ, โZโ, โ8โ]
filter not
(^) [True, False, False, False, True]