Created
November 4, 2019 11:16
-
-
Save jordi-petit/dd6cd4524c0e5cbcee41ecddd1d0ecbd to your computer and use it in GitHub Desktop.
Solució parcial LP de Haskell 2019-11-04
This file contains 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
-- string to int conversion | |
s2i :: String -> Int | |
s2i = read | |
add :: String -> String | |
add = show . sum . (map s2i) . words | |
main = do | |
contents <- getContents | |
putStrLn $ add contents |
This file contains 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
import Data.List | |
-- a) | |
-- funció auxiliar que diu si un node és un extrem d'una aresta | |
endPoint :: Eq a => a -> (a, a) -> Bool | |
endPoint node (u, v) = node == u || node == v | |
degree :: Eq a => [(a, a)] -> a -> Int | |
degree [] _ = 0 | |
degree (edge:edges) node | |
| endPoint node edge = degree edges node + 1 | |
| otherwise = degree edges node | |
-- b) | |
degree' :: Eq a => [(a, a)] -> a -> Int | |
degree' edges node = length $ filter (endPoint node) edges | |
-- c) | |
neighbors :: Ord a => [(a, a)] -> a -> [a] | |
neighbors edges node = sort $ map other $ filter (endPoint node) edges | |
where other (u, v) = if u == node then v else u | |
This file contains 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
data Tree a = Empty | Node a (Tree a) (Tree a) | |
-- a) | |
instance Show a => Show (Tree a) where | |
show Empty = "()" | |
show (Node x t1 t2) = "(" ++ show t1 ++ "," ++ show x ++ "," ++ show t2 ++ ")" | |
-- b) | |
instance Functor Tree where | |
fmap f Empty = Empty | |
fmap f (Node x t1 t2) = Node (f x) (fmap f t1) (fmap f t2) | |
doubleT :: Num a => Tree a -> Tree a | |
doubleT = fmap (*2) | |
-- c) | |
data Forest a = Forest [Tree a] | |
deriving (Show) | |
instance Functor Forest where | |
fmap f (Forest ts) = Forest $ map (fmap f) ts | |
doubleF :: Num a => Forest a -> Forest a | |
doubleF = fmap (*2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment