Created
November 6, 2017 13:25
-
-
Save jordi-petit/e229e26dcceca7941ac1fcada71ee286 to your computer and use it in GitHub Desktop.
LP 2017-11-06 Tipus
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
-- Joc de pedra paper o tisora | |
data Jugada = Pedra | Paper | Tisora | |
guanya :: Jugada -> Jugada -> Bool | |
Pedra `guanya` Tisora = True | |
Tisora `guanya` Paper = True | |
Paper `guanya` Pedra = True | |
_ `guanya` _ = False | |
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
-- Els booleans estàndards deuen ser alguna cosa així: | |
data Bool = False | True |
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
-- Llistes d'enters | |
data LlistaEnters | |
= Buida | |
| Int `Davant` LlistaEnters | |
llista1 = 3 `Davant` (4 `Davant` Buida) | |
llista2 = 5 `Davant` llista1 | |
llargada :: LlistaEnters -> Int | |
llargada Buida = 0 | |
llargada (_ `Davant` ll) = 1 + llargada ll | |
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
-- Llistes genèriques | |
data Llista a | |
= Buida | |
| a `Davant` LlistaEnters | |
llista1 = 3 `Davant` (4 `Davant` Buida) | |
llista2 = 5 `Davant` llista1 | |
llargada :: Llista -> Int | |
llargada Buida = 0 | |
llargada (_ `Davant` ll) = 1 + llargada ll | |
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
-- Les llistes estàndards deuen ser alguna cosa així: | |
data [a] = [] | a:[a] |
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
-- Arbres binaris genèrics | |
data Arbin a | |
= Buit | |
| Node a (Arbin a) (Arbin a) | |
arbre1 = Node 6 (Node 3 Buit Buit) (Node 2 Buit Buit) | |
alçada :: Arbin a -> Int | |
alçada Buit = 0 | |
alçada (Node _ fe fd) = 1 + max (alçada fe) (alçada fd) | |
mida :: Arbin a -> Int | |
mida Buit = 0 | |
mida (Node _ fe fd) = 1 + mida fe + mida fd |
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
-- Arbres generals genèrics | |
data Argal a | |
= Buit | |
| Node a [Argal a] | |
arbre1 = Node 6 [Node 3 Buit Buit, Node 2 Buit Buit, Node 5 Buit Buit] | |
alçada :: Arbin a -> Int | |
alçada Buit = 0 | |
alçada (Node _ fills) = 1 + maximum $ map alçada fills | |
mida :: Arbin a -> Int | |
mida Buit = 0 | |
mida (Node _ fills) = 1 + sum $ map mida fills |
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
-- Expressions genèriques | |
data Expr a | |
= Val a | |
| Neg (Expr a) | |
| Add (Expr a) (Expr a) | |
| Sub (Expr a) (Expr a) | |
| Mul (Expr a) (Expr a) | |
| Div (Expr a) (Expr a) | |
expr1 = Neg (Add (Val 3) (Mul (Val 2) (Val 6))) | |
eval :: Expr a -> a | |
eval (Val x) = x | |
eval (Neg e) = -e | |
eval (Add e1 e2) = e1 + e2 | |
eval (Sub e1 e2) = e1 - e2 | |
eval (Mul e1 e2) = e1 * e2 | |
eval (Div e1 e2) = e1 / e2 |
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
-- Tipus estàndards | |
data Maybe a = Just a | Nothing | |
data Either a b = Left a | Right b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment