Haskell LP 5 (2014)

Apunte Español
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 2
Fecha de subida 06/12/2014
Descargas 3
Subido por

Vista previa del texto

flatten::[[Int]] -> [Int] flatten n = foldl (++) [] n myLength::String -> Int myLength [] = 0 myLength (h:st) = 1 + (myLength st) myReverse::[Int] -> [Int] myReverse [] = [] myReverse (x:xs) = (myReverse xs) ++ [x] conta _ [] = 0 conta n (x:xs) | n == x = 1 + conta n xs | otherwise = conta n xs countIn::[[Int]] -> Int -> [Int] countIn [] x = [] countIn (l:ls) x = map (conta x) (l:ls) firstWord::String -> String firstWord str = takeWhile (\x -> x /= ' ') (dropWhile (\x -> x == ' ' ) str) countIf::(Int->Bool) -> [Int] -> Int countIf _ [] = 0 countIf f (x:xs) | (f x) = 1 + (countIf f xs) | otherwise = (countIf f xs) aplica _ [] = [] aplica f (x:xs) = (f x):(aplica f xs) pam::[Int]->[Int -> Int] -> [[Int]] pam l lf = (map (\f -> aplica f l ) lf) aplicaA [] _ = [] aplicaA (f:lf) x = (f x):(aplicaA lf x) pam2::[Int]->[Int -> Int] -> [[Int]] pam2 l lf = (map (\x -> aplicaA lf x) l) -- pam l lf = [ (f x) | x <- l, f <- lf] filterFoldl::(Int->Bool) -> (Int -> Int -> Int) -> Int -> [Int] -> Int filterFoldl _ _ acc [] = acc filterFoldl bf f acc (x:xs) | (bf x) = (filterFoldl bf f (f acc x) xs) | otherwise = (filterFoldl bf f acc xs) insert bf l e = (takeWhile (\x -> bf x e) l) ++ [e] ++ (dropWhile (\x -> bf x e) l) insertionSort _ [] = [] insertionSort bf (x:xs) = (insert bf acc x) where acc = (insertionSort bf xs) -- countIf (>5) [1..10] -- pam [1,2,3] [(+1),(*2),(^2)] -- pam2 [1,2,3] [(+1),(*2),(^2)] -- filterFoldl even (*) 1 [4,7,2,4,9,3] -- insert (<) [1,4,6,9,12] 8 -- insertionSort (>) [4,5,2,3,1,3] --- 5 -- [[2,3,4],[2,4,6],[1,4,9]] -- [[2,2,1],[3,4,4],[4,6,9]] -- 32 -- [1,4,6,8,9,12] -- [5,4,3,3,2,1] Powered by TCPDF (www.tcpdf.org) ...