Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p1
- halveEvens :: [Int] -> [Int]
- halveEvens xs = [x `div` 2 | x<-xs, even x ]
- p2
- halveEvensRec :: [Int] -> [Int]
- halveEvensRec [] = []
- halveEvensRec (x:xs) | even x = div x 2 : halveEvensRec (xs)
- | otherwise = halveEvensRec(xs)
- p3
- inRange :: Int -> Int -> [Int] -> [Int]
- inRange lo hi xs = [ x| x<-xs,x>=lo, x<=hi]
- p4
- inRangeRec :: Int -> Int -> [Int] -> [Int]
- inRangeRec lo hi [] = []
- inRangeRec lo hi (x:xs) | x<=hi && x>= lo = x : inRangeRec lo hi xs
- | otherwise = inRangeRec lo hi xs
- p5
- countPositives :: [Int] -> Int
- countPositives xs = length [ x | x<- xs, x>0 ]
- p6
- countPositivesRec :: [Int] -> Int
- countPositivesRec [] = length []
- countPositivesRec (x:xs) | x<=0 = countPositivesRec(xs)
- | otherwise = length[x] + countPositivesRec(xs)
- p6'
- countPositivesRec :: [Int] -> Int
- countPositivesRec [] = 0
- countPositivesRec (x:xs) = (if x<=0 then 0 else 1) + countPositivesRec(xs)
- p7.//copied from solution // BEWARE OF COPYING
- -- Helper function
- discount :: Int -> Int
- discount price = round(0.9 * fromIntegral price)
- -- List-comprehension version of pennypincher
- pennypincher :: [Int] -> Int
- pennypincher prices = sum [discount price |price <- prices, discount price <=19900 ]
- p8
- -- Helper function
- discount :: Int -> Int
- discount price = round(0.9 * fromIntegral price)
- -- Recursive version
- pennypincherRec :: [Int] -> Int
- pennypincherRec [] = 0
- pennypincherRec (price:prices) =(if discount price <=19900 then discount price else 0) + pennypincherRec(prices)
- p9
- multDigits :: String -> Int
- multDigits str = product[ (if isDigit(x) then digitToInt(x) else 1) | x<-str]
- p9' smart definition using ord[which tells the ascii no of a particular item]
- multDigits cs = product [ ord c - ord '0' | c <- cs, isDigit c] like ord '0' = 48
- p10
- multDigitsRec :: String -> Int
- multDigitsRec [] = 1
- multDigitsRec (x:xs) = (if isDigit x then ord x - ord '0' else 1) * multDigitsRec(xs)
- p10'
- multDigitsRec :: String -> Int
- multDigitsRec [] = 1
- multDigitsRec (x:xs) | isDigit x = digitToInt x * multDigitsRec xs
- | otherwise = 1 * multDigitsRec xs
- p11
- capitalise :: String -> String
- capitalise "" = ""
- capitalise (x:xs) = toUpper x : [toLower x' | x'<-xs ]
- p12
- -- Recursive version
- capitaliseRec :: String -> String
- capitaliseRec [] = ""
- capitaliseRec (x:xs) = toUpper x : lowerRec xs
- -- Helper function
- lowerRec :: String -> String
- lowerRec [] = ""
- lowerRec (x:xs) = toLower x : lowerRec xs
Add Comment
Please, Sign In to add comment