sakkat

Haskell

May 15th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. --Nome das funcoes: Fatorial (int x), Multiplicar(Int x, Int y), Mdc(Int x,Int y), sub(Int x, Int y). Os metodos subP e subN devem ser --privados
  2.  
  3. -- Fatorial
  4. -- Condição de saida
  5. Fatorial::Int->Int
  6. Fatorial 0 = 1
  7. -- Recursao
  8. Fatorial x = x * (Fatorial (x-1) )
  9.  
  10. -- multiplicações com somas repetidas
  11. Multiplicar :: Int -> Int -> Int
  12. -- Condição de saida
  13. Multiplicar x 1 = x
  14. -- Recursao
  15. Multiplicar x y = x+(Multiplicar x (y-1))
  16.  
  17. --Maximo divisor comum
  18. Mdc::Int->Int->Int
  19. -- Condição de inversão
  20. Mdc x y | x < y = Mdc y x
  21. --Condição de saida
  22. | y == 0 = b
  23. -- Recursao
  24. | otherwise = Mdc y (x 'mod' y)
  25.  
  26. -- Subtração por somas repetidas de 1 no resultado e no subtraendo, inverte caso seja negativo
  27. sub::Int->Int->Int
  28. sub x y
  29. --Saida simples
  30. | x == y = 0
  31. --Verifica se x é menor que Y pra fazer subtração de negativos
  32. | x < y = subN y x 0
  33. | otherwise = subP x y 0
  34.  
  35. --Subtração negativa, resultado multiplica por -1
  36. subN::Int->Int->Int->Int
  37. subN x y result
  38. --Condição de saida
  39. | x == y =result*(-1)
  40. --Recursao
  41. | otherwise=subN x (y+1) (result+1)
  42.  
  43. --Subtração positiva
  44. subP::Int->Int->Int->Int
  45. subP x y result
  46. --Condição de saida
  47. | x == y =result
  48. --Recursao
  49. | otherwise=subP x (y+1) (result+1)
Add Comment
Please, Sign In to add comment