Advertisement
tomasfdel

Estructuras II Práctica Lab 1

Mar 27th, 2018
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Lab01 where
  2.  
  3. import Data.List
  4.  
  5. -- 1-a) Cambiar los espacios de indentación
  6. not b = case b of
  7.              True -> False
  8.              False -> True
  9.  
  10. -- 1-b) "in" es palabra reservada
  11. borrar [x]     =  []
  12. borrar (x:xs)  =  x : borrar xs
  13. borrar []      =  error "empty list"
  14.  
  15. -- 1-c) Usar "length" en vez de "Length"
  16. length1 []        =  0
  17. length1 (_:l)     =  1 + length1 l
  18.  
  19. -- 1-d) (:) toma un elemento y una lista (en ese orden)
  20. list123 = (1 : (2 : (3 : [])))
  21.  
  22. -- e) Anda en Haskell reciente. Como funcion prefija necesita empezar con letra
  23. []     ++! ys = ys
  24. (x:xs) ++!  ys = x : xs ++! ys
  25.  
  26. --sumame a b = a+b
  27.  
  28. -- f) Problema con los paréntesis
  29. addToTail x xs = map (+x) (tail xs)
  30.  
  31. -- g) Idem anterior
  32. listmin xs = head (sort xs)
  33.  
  34. -- h) Dumb programmer.
  35. smap f [] = []
  36. smap f [x] = [f x]
  37. smap f (x:xs) = f x : smap f xs
  38.  
  39.  
  40.  
  41.  
  42.  
  43. -- 2-a) five :: Num a => t -> a
  44. five x = 5
  45.  
  46. -- 2-b) apply :: (a -> b) -> a -> b
  47. apply f x = f x
  48.  
  49. -- 2-c) ident :: a -> a
  50. ident x = x
  51.  
  52. -- 2-d) first :: (a,b) -> a
  53. first (x,y) = x
  54.  
  55. -- 2-e) derive :: (Fractional a, Fractional a1) => (a1 -> a) -> a1 -> a
  56. derive f x = (f x+0.1 - f x) / 0.1
  57.  
  58. -- 2-f) sign :: (Num a, Ord a, Num b) => a -> b
  59. sign x | x < 0   = -1
  60.        | x == 0  = 0
  61.        | otherwise = 1
  62.        
  63. -- 2-g) vabs1 :: (Num a, Ord a) => a -> a
  64. --      vabs2 :: (Num a, Ord a) => a -> a
  65. vabs1 x = x * sign x
  66.  
  67. vabs2 x | x <= 0     = -x
  68.         | otherwise  = x
  69.        
  70. -- 2-h) pot :: (Integral b, Num a) => b -> a -> a
  71. pot x y = y ^ x
  72.  
  73. -- 2-i) xor :: Bool -> Bool -> Bool
  74. xor True True = False
  75. xor False False = False
  76. xor _ _ = True
  77.  
  78. -- 2-j) max3 :: Ord a => a -> a -> a -> a
  79. max3 a b c = max a (max b c)
  80.  
  81. -- 2-k) swap :: (a,b) -> (b,a)
  82. swap (x,y) = (y,x)
  83.  
  84.  
  85. -- 3) bisiesto :: (Integral a) => a -> Bool
  86. bisiesto x | ((mod x 4) /= 0)                                 = False
  87.            | and [(mod x 100) == 0, (mod (div x 100) 4) /= 0] = False
  88.            | otherwise                                        = True
  89.  
  90.  
  91. -- 4)  *$ :: Num a => [a] -> a -> [a]
  92. [] *$ num     = []
  93. (x:xs) *$ num = (x*num) : (xs *$ num)
  94.  
  95. v = [1, 2, 3] *$ 2 *$ 4
  96.  
  97.  
  98. -- 5-a)
  99. divisors n = [x | x <- [1..n], mod n x == 0]
  100.  
  101. -- 5-b)
  102. matches x lista = [n | n <- lista, n == x]
  103.  
  104. -- 5-c)
  105. cuadrupla n = [(a,b,c,d) | a <- [1..n], b <- [1..n], c <- [1..n], d <- [1..n], a*a + b*b == c*c + d*d]
  106.  
  107. -- 5-d)
  108. unique xs = [x | (x,i) <- zip xs [0..], not(elem x (take i xs)) ]
  109.  
  110. -- 6)
  111. productupla (x,y) = x * y
  112. scalarProduct x y = sum (map productupla (zip x y))
  113.  
  114. -- 7-a) suma:: Num a => [a] -> a
  115. suma [] = 0
  116. suma (x:xs) = x + suma xs
  117.  
  118. -- 7-b) alguno::[Bool] -> Bool
  119. alguno [] = False
  120. alguno (x:xs) = x || alguno xs
  121.  
  122. -- 7-c) todos::[Bool] -> Bool
  123. todos [] = True
  124. todos (x:xs) = x && todos xs
  125.  
  126. -- 7-d) codes:: [Char] -> [Int]
  127. codes [] = []
  128. codes (x:xs) = ord x : codes xs
  129.  
  130. -- 7-e) restos:: Integral a => [a] -> a -> [a]
  131. restos [] numero = []
  132. restos (x:xs) numero = (mod x numero) : (restos xs numero)
  133.  
  134. -- 7-f) cuadrados:: Num a => [a] -> [a]
  135. cuadrados [] = []
  136. cuadrados (x:xs) = (x * x) : cuadrados xs
  137.  
  138. -- 7-g) longitudes:: [[a]] -> [Int]
  139. longitudes [] = []
  140. longitudes (x:xs) = length x : longitudes xs
  141.  
  142. -- 7-h) orden:: (Ord a, Num a) => [(a,a)] -> [(a,a)]
  143. orden [] = []
  144. orden ((x,y):xs) = if x < 3 * y then (x,y):orden xs else orden xs
  145.  
  146. -- 7-i) pares:: (Integral a) => [a] -> [a]
  147. pares [] = []
  148. pares (x:xs) = if mod x 2 == 0 then x:pares xs else pares xs
  149.  
  150. -- 7-j) letras:: [Char] -> [Char]
  151. letras [] = []
  152. letras (x:xs) = if ((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z')) then x:letras xs else letras xs
  153.  
  154. -- 7-k) masDe:: (Ord t) => [[a]] -> Int -> [[a]]
  155. masDe [] largo = []
  156. masDe (x:xs) largo = if length x > largo then x:masDe xs largo else masDe xs largo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement