# Exercicis de Haskell del Jutge (LP) (2014)

Ejercicio 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 3 Fecha de subida 26/06/2014 Descargas 7 Puntuación media Subido por kaito

### Descripción

Alguns exercicis de Haskell del Jutge Resolts.

### Vista previa del texto

-- absolut value absValue x | x >= 0 = x | otherwise = -x power x 0 = 1 power x p = x * power x (p-1) mod2 x = x`mod`2 mult x y | (x `mod` y) == 0 = True | otherwise = False isPrime 1 = False isPrime x = (isprime x (x-1)) isprime x 1 = True isprime x y | mult x y = False | otherwise = isprime x (y-1) --slowFib slowFib = sFib sFib 0 = 0 sFib 1 = 1 sFib x = sFib (x-1) + sFib (x-2) -- quickfib as a composition fo fst nd qfib quickFib = fst.qFib qFib 0 = (0, 1) qFib x = (b,a+b) where (a,b) = qFib(x-1) --returns the length of a list myLength[] = 0; myLength (x:xs) = ( 1 + (myLength xs) ) --returns the maximum number of the list myMaximum [x] = x myMaximum (x:xs) |x>t=x | otherwise = t where t = myMaximum xs --returns the average of a list -- use the sum function which sum the elements of the list --use the myLength function defined above suma[] = 0 suma(x:xs) = x + (suma xs) average l@(x:xs) = suma l / myLength l --returns the palindrome of the parameter list buildPalindrome [] = [] buildPalindrome (x:xs) = buildPalindrome xs ++[x] --returns the elements of the first list when removed --the elements of the second list --uses the function included which means the list inlcude the element included a [] = False included a (x:xs) | a == x = True | otherwise = included a xs remove [] elements = [] remove (x:xs) elements | included x elements = remove xs elements | otherwise = x:remove xs elements --retorna la llista de elements de cada llista de una llista de llistes flatten [] = [] flatten (x:xs) = x ++ flatten xs --donada una llista d'enters retorna dues llistes, una que --conte els parells i una que conte els senars en l'ordre original oddNevens [] = ([],[]) oddNevens (x:xs) | even x = (x:a, b) | odd x = (a, x:b) where (a,b) = oddNevens xs -- a function that given a sorted list and an element -- inserts it in it's position --insert::[int]->int->[int] insert [] e = [e] insert [x] e | e > x = x:e:[] | e < x = e:x:[] insert (x:y:xs) e | e < x = e:x:y:xs | x <= e && e <= y = x:e:y:xs | e > y = x:y:(insert xs e) --funcio eql::[Int] -> [Int] -> Bool --indica si les llistes son iguals eql::[Int] -> [Int] -> Bool eql [][] = True eql _ [] = False eql [] _ = False eql (x:xs) (y:ys) | x /= y = False | otherwise = eql xs ys -- producte dels elements de la llista passada prod::[Int] -> Int prod l = foldl (*) 1 l --prodofevents multiplica els nombres parells de una llista prodOfEvens::[Int] -> Int prodOfEvens l = foldl (*) 1 list where list = filter even l --powersOf2 genera la llista de les potències de dos powersOf2::[Integer] powersOf2 = iterate (\x -> x*2) 1 --scalar product que calcula el prod escalar de dues llistes --de reals de la mateixa mida scalarProduct::[Float] -> [Float] -> Float scalarProduct l l2 = sum(zipWith (*) l l2) -- apalana una llista de llistes d'enters en una llista d'enters flatten::[[Int]]->[Int] flatten [] = [] flatten (l:ll) = map (*1) (map (+0) (foldl (++) l ll)) -- Fagot eficient way --flatten (l:ll) = (foldl (++) l ll --my length retorna la llargada d'una cadena de caràcters myLength::String->Int myLength llista = foldl (\x y -> x+1) 0 llista --revers myReverse::[Int]-> [Int] myReverse [] = [] myReverse (x:xs) = myReverse xs ++ [x] ...