Advertisement
DimasDark

Haskell Tests

Oct 2nd, 2014
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Environment
  2.  
  3. ehPalindromo:: [Int] -> Bool
  4. ehPalindromo [] = True
  5. ehPalindromo a | a == reverse a = True
  6.                     |otherwise = False
  7.  
  8. main = print $ ehPalindromo [1,0,2,1]
  9.  
  10. import System.Environment
  11.  
  12. qPerfect:: Int -> [Int]
  13. qPerfect 0 = [0]
  14. qPerfect n = n^2 : qPerfect (n-1)
  15.  
  16. main = print $ qPerfect 5
  17.  
  18. import System.Environment
  19.  
  20. zipLists:: [a] -> [b] -> [c] -> [(a,b,c)]
  21. zipLists (x:xs) (y:ys) (z:zs) = (x,y,z):zipLists xs ys zs
  22. zipLists _ _ _ = []
  23.  
  24.  
  25. main = print $ zipLists [1,2,3] ['%','$','c'] ['f','g','3']
  26.  
  27. import Data.Char
  28. import Text.Printf
  29.  
  30. sumHex :: [Char] -> Int
  31. sumHex (x:xs) = ord x + sumHex xs
  32. sumHex [] = 0
  33.  
  34. main = do
  35.     putStrLn $ printf "Soma em hexadecimal: 0x%08x" $ sumHex ['a','b','c']
  36.  
  37. import Data.Char
  38. import Text.Printf
  39.  
  40. dobraTripla :: [Int] -> [Int]
  41. dobraTripla (x:xs) = | mod (length (x:xs)) 2 == 0 =
  42.                      dobraTripla[x^3 | mod (length (x:xs)) 2 /= 0]
  43. dobraTripla [] = [0]
  44.  
  45.  
  46. main :: IO()
  47. main = do
  48.     putStrLn (show ( dobraTripla[1,2,3,4,5,6]))
  49.  
  50. --------------------------------------------------------------------------------------------------------------------------------
  51. Lista 2 - 2013.1 - Q.2
  52. fatorial :: Int -> Int
  53. fatorial 0 = 1
  54. fatorial n = n * fatorial (n-1)
  55.  
  56. add :: Int -> Int
  57. add a = a+1
  58.  
  59. annexer :: (b->c) -> [a-> b] -> [a->c]
  60. annexer f [] = []
  61. annexer f (x:xs) = (f.x) : annexer f xs
  62.  
  63.  
  64. test [] val = []
  65. test (v:vs) val = (map v val):test vs val
  66.  
  67.  
  68. main :: IO()
  69. main = do
  70.          print $ test [fatorial,add,add] [5]      
  71.  
  72. --------------------------------------------------------------------------------------------------------------------------------
  73.  
  74.  
  75. setFilters :: (a->b->Bool) -> [a] -> [(b->Bool)]
  76. setFilters a (x:xs) = a x : map a xs
  77.  
  78. specificFilter :: [(a->Bool)] -> [a] -> [a]
  79. specificFilter l [] = []
  80. specificFilter [] l = []
  81. specificFilter (x:xs) (z:zs) | (x.z) == True =
  82.                 x ++ specificFilter xs zs
  83.                 otherwise = specificFilter xs zs
  84.  
  85. setMaps :: (a->b->c) -> [a] -> [(b->c)]
  86. setMaps a (x:xs) = a x : map a xs
  87.  
  88. specificMap :: [(a->b)] -> [a] -> [b]
  89. specificMap l [] = []
  90. specificMap [] l = []
  91. specificMap (f:fs) (x:xs) = map f x ++ specificaMap fs xs
  92.  
  93. specificApply :: [(b->Bool)] -> [(a->b)] -> [a] -> [b]
  94. specificApply
  95.  
  96. ---------------------------------------------------------------------------------------------------------------------------------
  97.  
  98.  
  99. -- | Main entry point to the application.
  100. module Main where
  101.  
  102. gerarListaCombinacoes :: Int -> Int -> [[Int]]
  103. gerarListaCombinacoes n 0 = [[]]
  104. gerarListaCombinacoes n k | n < k = [[]]
  105.                           |otherwise = [(x:xs) | x <- [0..n-1], xs <- (gerarListaCombinacoes n (k-1))]
  106.  
  107. main :: IO()
  108. main = do
  109.          print $ gerarListaCombinacoes 4 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement