Advertisement
CamolaZ

AulaRecursao

Mar 26th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sumPositiveOnList [] = 0
  2. sumPositiveOnList (x:xs) = (if x>0 then x else 0) + sumPositiveOnList xs
  3.  
  4. sumPositiveOnList' x = sum (filter (>0) x)
  5.  
  6. -- $ is for compose function and substitute the ( ),  example
  7. -- f1 (f2 (5*x))
  8. -- compose f1*f2 $ 5*x
  9. -- f1*f2 $ 5 *x  , and for cutting $ sign for cutting
  10.  
  11. sumPositiveOnList2 x = sum $ filter (>0) x
  12.  
  13. --2
  14. count :: Num p => (t -> Bool) -> [t] -> p
  15. count _ [] = 0
  16. count f (x:xs) = (if f x == True then 1 else 0) + count f xs
  17.  
  18. count' f (x:xs) = length $ filter f x -- or bracket way, without it , it's don't compile
  19. --count f (x:xs) = length $ filter f x, says:  Multiple declarations of `count'  
  20.  
  21. --3
  22. num2 = do
  23.  putStr "a = "
  24.  a <- getLine
  25.  putStr "b = "
  26.  b <- getLine
  27.  putStrLn $ "a+b = "++ a ++ "+" ++ b ++ "=" ++show((read a :: Double)+(read b::Double))
  28.  
  29.  --4
  30.  --sn _ "" = ""
  31. sN n s
  32.  | n < 0 = "Invalid value of n"
  33.  | n==0 = ""
  34.  | otherwise = s ++ " " ++ sN (n-1) s
  35.    
  36. sns = do
  37.  putStr "Give me a number n: "
  38.  n <- getLine
  39.  putStr "Type a string: "
  40.  s <- getLine
  41.  putStr $ sN (read n::Integer) s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement