Advertisement
CamolaZ

AulaP3 PLP

Mar 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. factors n = [i | i <-[1..n], n `mod` i ==0 ]
  2.  
  3. isPrime n = factors n == [1,n]
  4. primes =[n| n<-[2,3..], isPrime n == True]
  5.  
  6. nPrimes n = take n primes
  7.  
  8. sum' [] =0
  9. sum' (x:xs) = x + sum' xs
  10.  
  11. multiply  [] = 1
  12. multiply (x:xs) = x * multiply xs
  13.  
  14. c 1 = 1
  15. c n = 1+2* c (n-1)
  16.  
  17. listC = [c n | n<- [1..15]]
  18.  
  19. lisCN n =[c i | i <- [1..n]]
  20.  
  21. c1000 = takeWhile (<1000) [c n | n <- [1..]]
  22.  
  23. --Ex.4
  24. firstFromList [] = error "Empty list :) Be carefull"
  25. firstFromList (x:_) = x -- key not only characters, give the type of declaration ... not general~, for the Second from the list
  26.  
  27. secondFromList ::[a] -> a
  28. secondFromList [] = error "Empty!!!!"
  29. secondFromList [_] = error "Only one"
  30. secondFromList (_:x:_) = x
  31.  
  32. lastFromList :: [a]-> a
  33. lastFromList [] = error "Empty list!"
  34. lastFromList [x] = x
  35. lastFromList (x:xs) = lastFromList xs
  36.  
  37. {--
  38. 5 - Define a function
  39.  
  40.             mergeLists :: [[a]] -> [a]
  41.            
  42. which returns the combined list of lists of any elements.
  43.  
  44. For example:
  45.  
  46. mergeLists [[1,2,3],[4,8],[9]]
  47.  
  48. returns the list [1,2,3,4,8,9].
  49.  
  50. mergeLists :: [[a]] -> [a]
  51. mergeLists x = concat x
  52. above good!
  53. megelist
  54. mergeLists xs ++ [] = xs
  55. mergeList xs
  56. --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement