--1 misterio n (f,e) = (foldl (\x y -> f y x) e).(take n) -- misterio :: Int -> (a -> b -> b,b) -> [a] -> b -- (misterio n (f,e) xs) pliega a la izquierda los n primeros -- elementos de xs, utilizando como función de plegado f' = (\x y -> f y x) -- es decir la función que aplica f a sus argumentos en orden inverso, -- y como elemento neutro e. -- misterio 3 ((^), 1) [2,3,4,5,6] -- => 4^(3^(2^1)) -- => 262144 :: Integer --2 h f p g = (map f).(filter p).(map g) -- h :: (a -> b) -> (a -> Bool) -> (c -> a) -> [c] -> [b] h' f p g xs = [f y | y <- map g xs, p y] --3 serie :: Integral a => a -> [a] serie b = zs where zs = b : map (\x -> (x + b*x)`div`2) zs -- take 4 (serie 9) -- [9,45,225,1125] :: [Integer] -- (este es mal editor para hacer dibujos, se usa ! y @) {- serie 9 ! => 9 : map (\x -> (x + 9*x)`div`2) @ - ------------------------------ x xs ! => 9 : (\x -> (x + 9*x)`div`2) 9 : map (\x -> (x + 9*x)`div`2) @ ! => 9 : (9 + 9*9)`div`2 : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : map (\x -> (x + 9*x)`div`2) @ -- ------------------------------ x xs ! => 9 : 45 : ((\x -> (x + 9*x)`div`2) 45) : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : ((45 + 9*45)`div`2) : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : 225 : map (\x -> (x + 9*x)`div`2) @ --- ------------------------------ x xs ! => 9 : 45 : 225 : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : 225 : (\x -> (x + 9*x)`div`2) 225 : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : 225 : ((225 + 9*225)`div`2) : map (\x -> (x + 9*x)`div`2) @ ! => 9 : 45 : 225 : 1125 : map (\x -> (x + 9*x)`div`2) @ => ... -} -- 4 data Proposicion = Var String | F | T | Not Proposicion | Proposicion :\/: Proposicion | Proposicion :/\: Proposicion deriving Show ejemplo = Not ((Var "p" :\/: F) :/\: (Not (Var "q"))) esFNN :: Proposicion -> Bool -- esFNN (Var _) = True -- esFNN F = True -- esFNN T = True esFNN (Not (Var _)) = True esFNN (Not _) = False esFNN (p1 :\/: p2) = esFNN p1 && esFNN p2 esFNN (p1 :/\: p2) = esFNN p1 && esFNN p2 esFNN _ = True -- sustituye a las tres de arriba, pero debe estar al final aFNN :: Proposicion -> Proposicion aFNN (Not T) = F aFNN (Not F) = T aFNN (Not(Not p)) = p aFNN (Not(p1 :/\: p2)) = aFNN(Not p1) :\/: aFNN(Not p2) aFNN (Not(p1 :\/: p2)) = aFNN(Not p1) :/\: aFNN(Not p2) aFNN (p1 :/\: p2) = aFNN p1 :/\: aFNN p2 aFNN (p1 :\/: p2) = aFNN p1 :\/: aFNN p2 aFNN p = p instance Eq Proposicion where p1 == p2 = iguales (aFNN p1) (aFNN p2) where iguales (Var s1) (Var s2) = s1==s2 iguales T T = True iguales F F = True iguales (Not p1) (Not p2) = iguales p1 p2 iguales (p1 :/\: p2) (p1' :/\: p2') = iguales p1 p1' && iguales p2 p2' iguales (p1 :\/: p2) (p1' :\/: p2') = iguales p1 p1' && iguales p2 p2' iguales _ _ = False -- Para hacer pruebas ejemplo' = Not ((Var "p" :\/: T) :/\: (Var "q")) ejemplo'' = (Not (Var "p") :/\: T) :\/: (Var "q")