Guest User

Untitled

a guest
Feb 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. p1
  2. halveEvens :: [Int] -> [Int]
  3. halveEvens xs = [x `div` 2 | x<-xs, even x ]
  4.  
  5. p2
  6. halveEvensRec :: [Int] -> [Int]
  7. halveEvensRec [] = []
  8. halveEvensRec (x:xs) | even x = div x 2 : halveEvensRec (xs)
  9. | otherwise = halveEvensRec(xs)
  10.  
  11. p3
  12. inRange :: Int -> Int -> [Int] -> [Int]
  13. inRange lo hi xs = [ x| x<-xs,x>=lo, x<=hi]
  14.  
  15. p4
  16. inRangeRec :: Int -> Int -> [Int] -> [Int]
  17. inRangeRec lo hi [] = []
  18. inRangeRec lo hi (x:xs) | x<=hi && x>= lo = x : inRangeRec lo hi xs
  19. | otherwise = inRangeRec lo hi xs
  20.  
  21. p5
  22. countPositives :: [Int] -> Int
  23. countPositives xs = length [ x | x<- xs, x>0 ]
  24.  
  25. p6
  26. countPositivesRec :: [Int] -> Int
  27. countPositivesRec [] = length []
  28. countPositivesRec (x:xs) | x<=0 = countPositivesRec(xs)
  29. | otherwise = length[x] + countPositivesRec(xs)
  30. p6'
  31. countPositivesRec :: [Int] -> Int
  32. countPositivesRec [] = 0
  33. countPositivesRec (x:xs) = (if x<=0 then 0 else 1) + countPositivesRec(xs)
  34.  
  35. p7.//copied from solution // BEWARE OF COPYING
  36. -- Helper function
  37. discount :: Int -> Int
  38. discount price = round(0.9 * fromIntegral price)
  39.  
  40. -- List-comprehension version of pennypincher
  41. pennypincher :: [Int] -> Int
  42. pennypincher prices = sum [discount price |price <- prices, discount price <=19900 ]
  43.  
  44. p8
  45. -- Helper function
  46. discount :: Int -> Int
  47. discount price = round(0.9 * fromIntegral price)
  48.  
  49. -- Recursive version
  50. pennypincherRec :: [Int] -> Int
  51. pennypincherRec [] = 0
  52. pennypincherRec (price:prices) =(if discount price <=19900 then discount price else 0) + pennypincherRec(prices)
  53.  
  54.  
  55. p9
  56. multDigits :: String -> Int
  57. multDigits str = product[ (if isDigit(x) then digitToInt(x) else 1) | x<-str]
  58. p9' smart definition using ord[which tells the ascii no of a particular item]
  59. multDigits cs = product [ ord c - ord '0' | c <- cs, isDigit c] like ord '0' = 48
  60.  
  61.  
  62. p10
  63. multDigitsRec :: String -> Int
  64. multDigitsRec [] = 1
  65. multDigitsRec (x:xs) = (if isDigit x then ord x - ord '0' else 1) * multDigitsRec(xs)
  66.  
  67. p10'
  68. multDigitsRec :: String -> Int
  69. multDigitsRec [] = 1
  70. multDigitsRec (x:xs) | isDigit x = digitToInt x * multDigitsRec xs
  71. | otherwise = 1 * multDigitsRec xs
  72.  
  73.  
  74.  
  75. p11
  76. capitalise :: String -> String
  77. capitalise "" = ""
  78. capitalise (x:xs) = toUpper x : [toLower x' | x'<-xs ]
  79.  
  80.  
  81. p12
  82. -- Recursive version
  83. capitaliseRec :: String -> String
  84. capitaliseRec [] = ""
  85. capitaliseRec (x:xs) = toUpper x : lowerRec xs
  86.  
  87. -- Helper function
  88. lowerRec :: String -> String
  89. lowerRec [] = ""
  90. lowerRec (x:xs) = toLower x : lowerRec xs
Add Comment
Please, Sign In to add comment