Created
September 10, 2009 15:54
-
-
Save jsoffer/184620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
data Registro = R { | |
valor :: Integer, | |
nombre :: String, | |
peso :: Float | |
} deriving Show | |
data Arbol a = Hoja | Rama a a deriving Show | |
data Ternario = Negativo | Cero | Positivo deriving (Show, Eq, Ord) | |
(|*|) :: Ternario -> Ternario -> Ternario | |
(|*|) Cero _ = Cero | |
(|*|) _ Cero = Cero | |
(|*|) Negativo Positivo = Negativo | |
(|*|) Positivo Negativo = Negativo | |
(|*|) Positivo Positivo = Positivo | |
(|*|) Negativo Negativo = Positivo | |
(|**|) x y = if x == Cero || y == Cero then Cero else | |
if x == y then Positivo else Negativo | |
type ListaDeEnteros = [Integer] | |
f :: Integer -> Integer | |
f x = case x of | |
0 -> 0 | |
1 -> 10 | |
_ -> 400 | |
g :: Registro -> String | |
g x = case x of | |
R _ _ 0 -> "Tiene peso cero" | |
R 42 _ _ -> "Es 42" | |
R _ "hola" _ -> "Es un hola" | |
R _ _ _ -> "Otra cosa" | |
factorial :: Integer -> Either String Integer | |
factorial n = if n > 0 then Right (fact' n) else Left "n menor que uno" | |
where | |
fact' :: Integer -> Integer | |
fact' x | |
| x == 1 = 1 | |
| x > 1 = x * fact' (x-1) | |
| x < 1 = error "Factorial de valor menor que uno" | |
mapa f lista@(x:xs) = f (head lista) : mapa xs | |
mapa _ [] = [] | |
verificaFactorial :: Either String Integer -> String | |
verificaFactorial x = case x of | |
Right x -> "Todo sale bien. " ++ show x | |
Left x -> "Hubo un error: " ++ x | |
divisiblePor :: Integer -> Integer -> Bool | |
divisiblePor n x = mod x n == 0 | |
divisiblePor3 x = (divisiblePor 3) | |
lista :: ListaDeEnteros | |
lista = [ x | x <- [1..999], ((divisiblePor 3) x || (divisiblePor 5) x) ] | |
suma :: ListaDeEnteros -> Integer | |
suma x = sum x | |
main :: IO () | |
main = putStrLn $ show $ suma lista |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment