LP Haskell Exercicis de LListes per Comprensió (2014)

Ejercicio Catalán
Universidad Universidad Politécnica de Cataluña (UPC)
Grado Ingeniería Informática - 3º curso
Asignatura LP (Llenguatges de Programació)
Año del apunte 2014
Páginas 1
Fecha de subida 26/06/2014
Descargas 2
Subido por

Descripción

Exercicis del Jutge Resolts
de Llistes per comprensió amb Haskell

Vista previa del texto

myMap::(a->b) -> [a] -> [b] myMap f l = [ f x | x <- l] -- myMap [] = [] -- myMap f (x:xs) = (f x):(myMap f xs) myFilter::(a->Bool) -> [a] -> [a] myFilter bf l = [x | x <- l, (bf x)] -- myFilter _ [] = [] -- myFilter bf (x:xs) -- | bf = x:(myFilter xs) -- | otherwise myFilter xs myZipWith::(a -> b -> c) -> [a] -> [b] -> [c] myZipWith f la lb = [f (fst a) (fst b) | a <- (zip la [1..]) , b <- ( zip lb [1..]), (snd b) == (snd a) ] thingify::[Int] -> [Int] -> [(Int, Int)] thingify fl sl = [(x,y) | x <- fl, y <- sl, (x `mod` y) == 0] factors::Int -> [Int] factors n = [x | x <- [1..n], (n `mod` x) == 0] {myMap (*2) [1..5] myFilter odd [1..5] myZipWith (*) [1..4] [1..4] thingify [1..6] [1..3] factors 24 [2,4,6,8,10] [1,3,5] [1,4,9,16] [(1,1),(2,1),(2,2),(3,1),(3,3),(4,1),(4,2),(5,1),(6,1),(6,2),(6,3)] [1,2,3,4,6,8,12,24]-} ...