Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Lab01 where
- import Data.List
- -- 1-a) Cambiar los espacios de indentación
- not b = case b of
- True -> False
- False -> True
- -- 1-b) "in" es palabra reservada
- borrar [x] = []
- borrar (x:xs) = x : borrar xs
- borrar [] = error "empty list"
- -- 1-c) Usar "length" en vez de "Length"
- length1 [] = 0
- length1 (_:l) = 1 + length1 l
- -- 1-d) (:) toma un elemento y una lista (en ese orden)
- list123 = (1 : (2 : (3 : [])))
- -- e) Anda en Haskell reciente. Como funcion prefija necesita empezar con letra
- [] ++! ys = ys
- (x:xs) ++! ys = x : xs ++! ys
- --sumame a b = a+b
- -- f) Problema con los paréntesis
- addToTail x xs = map (+x) (tail xs)
- -- g) Idem anterior
- listmin xs = head (sort xs)
- -- h) Dumb programmer.
- smap f [] = []
- smap f [x] = [f x]
- smap f (x:xs) = f x : smap f xs
- -- 2-a) five :: Num a => t -> a
- five x = 5
- -- 2-b) apply :: (a -> b) -> a -> b
- apply f x = f x
- -- 2-c) ident :: a -> a
- ident x = x
- -- 2-d) first :: (a,b) -> a
- first (x,y) = x
- -- 2-e) derive :: (Fractional a, Fractional a1) => (a1 -> a) -> a1 -> a
- derive f x = (f x+0.1 - f x) / 0.1
- -- 2-f) sign :: (Num a, Ord a, Num b) => a -> b
- sign x | x < 0 = -1
- | x == 0 = 0
- | otherwise = 1
- -- 2-g) vabs1 :: (Num a, Ord a) => a -> a
- -- vabs2 :: (Num a, Ord a) => a -> a
- vabs1 x = x * sign x
- vabs2 x | x <= 0 = -x
- | otherwise = x
- -- 2-h) pot :: (Integral b, Num a) => b -> a -> a
- pot x y = y ^ x
- -- 2-i) xor :: Bool -> Bool -> Bool
- xor True True = False
- xor False False = False
- xor _ _ = True
- -- 2-j) max3 :: Ord a => a -> a -> a -> a
- max3 a b c = max a (max b c)
- -- 2-k) swap :: (a,b) -> (b,a)
- swap (x,y) = (y,x)
- -- 3) bisiesto :: (Integral a) => a -> Bool
- bisiesto x | ((mod x 4) /= 0) = False
- | and [(mod x 100) == 0, (mod (div x 100) 4) /= 0] = False
- | otherwise = True
- -- 4) *$ :: Num a => [a] -> a -> [a]
- [] *$ num = []
- (x:xs) *$ num = (x*num) : (xs *$ num)
- v = [1, 2, 3] *$ 2 *$ 4
- -- 5-a)
- divisors n = [x | x <- [1..n], mod n x == 0]
- -- 5-b)
- matches x lista = [n | n <- lista, n == x]
- -- 5-c)
- 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]
- -- 5-d)
- unique xs = [x | (x,i) <- zip xs [0..], not(elem x (take i xs)) ]
- -- 6)
- productupla (x,y) = x * y
- scalarProduct x y = sum (map productupla (zip x y))
- -- 7-a) suma:: Num a => [a] -> a
- suma [] = 0
- suma (x:xs) = x + suma xs
- -- 7-b) alguno::[Bool] -> Bool
- alguno [] = False
- alguno (x:xs) = x || alguno xs
- -- 7-c) todos::[Bool] -> Bool
- todos [] = True
- todos (x:xs) = x && todos xs
- -- 7-d) codes:: [Char] -> [Int]
- codes [] = []
- codes (x:xs) = ord x : codes xs
- -- 7-e) restos:: Integral a => [a] -> a -> [a]
- restos [] numero = []
- restos (x:xs) numero = (mod x numero) : (restos xs numero)
- -- 7-f) cuadrados:: Num a => [a] -> [a]
- cuadrados [] = []
- cuadrados (x:xs) = (x * x) : cuadrados xs
- -- 7-g) longitudes:: [[a]] -> [Int]
- longitudes [] = []
- longitudes (x:xs) = length x : longitudes xs
- -- 7-h) orden:: (Ord a, Num a) => [(a,a)] -> [(a,a)]
- orden [] = []
- orden ((x,y):xs) = if x < 3 * y then (x,y):orden xs else orden xs
- -- 7-i) pares:: (Integral a) => [a] -> [a]
- pares [] = []
- pares (x:xs) = if mod x 2 == 0 then x:pares xs else pares xs
- -- 7-j) letras:: [Char] -> [Char]
- letras [] = []
- letras (x:xs) = if ((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z')) then x:letras xs else letras xs
- -- 7-k) masDe:: (Ord t) => [[a]] -> Int -> [[a]]
- masDe [] largo = []
- 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