Last active
October 23, 2017 12:31
-
-
Save jordi-petit/0ef3c8273676e37c2d92da975365733e to your computer and use it in GitHub Desktop.
LP 2017-10-23
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
-- Diccionari de Strings a Ints amb funcions d'ordre superior | |
-- i valors per defecte. | |
type Dict = (String -> Int) | |
-- Versió 1 | |
create :: Int -> Dict | |
create def = \key -> def | |
search :: Dict -> String -> Int | |
search dic key = dic key | |
insert :: Dict -> String -> Int -> Dict | |
insert dic key val = \x -> if key == x then val else search dic x | |
-- Versió 2 | |
create :: Int -> Dict | |
create = const | |
search :: Dict -> String -> Int | |
search = ($) | |
insert :: Dict -> String -> Int -> Dict | |
insert dic key val x | |
| key == x = val | |
| otherwise = dic x | |
-- Exercici: Afegir el erase | |
erase :: Dict -> String -> Dict |
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 per comprensió | |
doblats xs = [2*x | x <- xs] | |
doblats xs = [d | x <- xs, let d = 2*x] | |
doblatsIMultiplesDe3 xs = [2*x | x <- xs, x `mod` 3 == 0] | |
prodCart n = [(i, j) | i <- [1..n], j <- [1..n]] | |
tripletesPitagoriques n = [(i, j, k) | i <- [1..n], j <- [i..n], k <- [j..n], i*i + j*j = k*k] | |
-- gens eficient, però per què es vegi com usar | |
-- (prodCart sense llistes per comprensió) | |
prodCart n = concatMap (f n) [1..n] -- concatMap vé a ser concat . map | |
f n i = zip (repeat i) [1..n] | |
-- o | |
prodCart n = concatMap (\i -> zip (repeat i) [1..n]) [1..n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment