-- ------------------------------------------------- -- SOLUCIONES PR. FUNCIONAL CONTROL 1 (3.4.07) -- ------------------------------------------------- module Control1 where -- 1. Dar una implementación posible en Haskell de la función f para que -- sea válida la expresión -- -- f 1 (if (f 0 []) then “si” else “no” ) -- -- con 3 condiciones: 1) debe ser polimórfica, 2) debe ser recursiva, -- y 3) debe usar todos sus parámetros. Según ésta implementación, -- ¿qué vale la expresión?. f :: Integer -> [a] -> Bool f _ [] = True f 0 _ = False f n (h:t) = f (n-1) t -- 2. Implementar una función Haskell que comprueba si un entero positivo -- es capicúa. Generalizarla para una lista de números. capicua :: Integer -> Bool capicua n = (r == reverse r) where r = cifras n cifras :: Integer -> [Integer] cifras n = if (n<10) then [n] else (mod n 10) : cifras (div n 10) -- generalizarla -- 3. Implementar en Haskel una función para pasar a base 10 un entero -- que está en una determinada base b (que puede ser mayor que 10). -- El número inicial se da como un string. Por ejemplo, si pasamos -- “101” en base 2, daría 5; si pasamos “2AC” en base 16 saldría -- 684, etc. dBase :: String -> Integer -> Integer dBase s b = dB (valores s) b dB :: [Integer] -> Integer -> Integer dB [] _ = 0 dB (h:t) b = h*b^(length t) + dB t b valores :: String -> [Integer] valores [] = [] valores (h:t) = (lugar h lista) : valores t lugar :: Char -> String -> Integer lugar x (h:t) = if x==h then 0 else 1 + (lugar x t) lista :: String lista = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ"