Mayor prefijo común
Definir la función
1 |
mayorPrefijoComun :: Eq a => [a] -> [a] -> [a] |
tal que (mayorPrefijoComun xs ys) calcula el mayor prefijo común a xs e ys. Por ejemplo,
1 2 3 4 |
mayorPrefijoComun "masa" "madre" == "ma" mayorPrefijoComun "masa" "padre" == "" mayorPrefijoComun "hola" "hielo" == "h" mayorPrefijoComun "helado" "heladeria" == "helad" |
Soluciones
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-- 1ª solución mayorPrefijoComun :: Eq a => [a] -> [a] -> [a] mayorPrefijoComun _ [] = [] mayorPrefijoComun [] _ = [] mayorPrefijoComun (x:xs) (y:ys) | x == y = x : mayorPrefijoComun xs ys | otherwise = [] -- 2ª solución mayorPrefijoComun2 :: Eq a => [a] -> [a] -> [a] mayorPrefijoComun2 xs ys = [x | (x,_) <- takeWhile iguales (zip xs ys)] where iguales (x,y) = x == y |
Pensamiento
Los ojos por que suspiras,
sábelo bien,
los ojos en que te miras
son ojos porque te ven.Antonio Machado
Una solución sin recursión.