-- 1 class Show a => MiClase a where dimension :: a -> Int instance MiClase Int where dimension _ = 1 -- instance Show a => MiClase [a] where -- dimension = length instance MiClase a => MiClase[a] where dimension = foldr (+) 0 . (map dimension) -- 2 f h p xs ys = [h x y | x<-xs , p x, y <-ys] -- f :: (a -> b -> c) -> (a -> Bool) -> [a] -> [b] -> [c] -- Main> f (+) even [1..4] [10..14] -- [12,13,14,15,16,14,15,16,17,18] f' h p xs ys = aplicar h (filter p xs) ys where aplicar _ [] _ = [] aplicar f (x:xs) ys = (map (f x) ys) ++ (aplicar f xs ys) -- 3 data Prop = Atomo Char | ConU ConUnario Prop | ConD ConBinario Prop Prop data ConUnario = No data ConBinario = Conj | Disy | Imp | DImp prop1 = ConU No (ConD Disy (Atomo 'p') (ConD Conj (Atomo 'q') (ConU No (Atomo 'r')))) foldP :: (Char -> a) -> (ConUnario -> a -> a) -> (ConBinario -> a -> a -> a) -> Prop -> a foldP f g1 g2 (Atomo c) = f c foldP f g1 g2 (ConU con1 p) = g1 con1 (foldP f g1 g2 p) foldP f g1 g2 (ConD con2 p1 p2) = g2 con2 (foldP f g1 g2 p1) (foldP f g1 g2 p2) subs1 = [('p',False),('q',False),('r',True)] evalP:: Prop -> [(Char,Bool)] -> Bool -- Pre: en subs hay exactamente un par (c,b) para cada c tal que (Atomo c) está en p evalP p subs = foldP (aplicar subs) ap1 ap2 p where aplicar subs c = (snd.head) [(x,y) |(x,y)<-subs, x==c] ap1 No b = not b ap2 Conj b1 b2 = b1 && b2 ap2 Disy b1 b2 = b1 || b2 ap2 Imp b1 b2 = (not b1) || b2 ap2 DImp b1 b2 = ((not b1) || b2) && ((not b2) || b1) -- Main> evalP prop1 subs1 -- True -- 4 type Almacen = [Pack] data Pack= PA (Ref, Marca, Consola, Juegos, Complementos, Precio) type Juegos = [String] type Complementos = [String] data Marca = Nintendo | Sony | MicroSoft deriving (Enum, Eq, Show) type Consola = String type Ref = Integer type Precio = Float p1,p2,p3,p4,p5,p6,p7 :: Pack p1 = PA (1111, Nintendo, "DS Lite", ["Big Brain Academy" ], [], 159.00) p5 = PA (1112, Nintendo, "DS Lite", ["Eragon" ], ["Bolsa", "2 stylus"], 155.90) p2 = PA (1116, Sony, "PSP", ["WRC", "Medieval Resurrection"], ["Estuche", "Tarjeta de memoria", "Funda", "Auriculares"], 154.90) p3 = PA (2254, Nintendo, "Wii", ["Sports"], ["Mando control remoto", "Numchaku"], 279.90) p4 = PA (1113, Nintendo, "GameBoy Advance", [], ["Bolsa", "Auriculares", "Adaptador USB"], 114.90) p6 = PA (2002, MicroSoft, "XBOX 360 PRO", ["Gears of war", "FIFA 06 Copa mundial"], ["Kit carga"], 419.00) p7 = PA (2002, MicroSoft, "XBOX 360 PRO", [], ["Disco duro", "2 mandos inalambricos","Mando DVD", "Auriculares", "Microfono"], 419.00) db = [p1,p2,p3,p4,p5,p6,p7] loMasEco:: Almacen -> [Pack] loMasEco al = [masEco i al |i<- [Nintendo .. MicroSoft]] masEco m = foldr1 barato.filter ((m==).suConsola) where suConsola (PA (_,c,_,_,_,_)) = c barato (PA (r1,m1,c1,j1,e1,p1)) (PA (r2,m2,c2,j2,e2,p2)) = if p1<=p2 then (PA (r1,m1,c1,j1,e1,p1)) else (PA (r2,m2,c2,j2,e2,p2)) -- Para poder ver resultados: instance Show Pack where show (PA (r1,_,c1,j1,e1,p1)) = show r1++" " ++ lerrokatu c1++" "++ show p1++ " "++ show (length j1) ++" "++ show (length e1)++"\n" lerrokatu xs = if length xs>=20 then (take 20 xs) else (xs++zuriak (20-length xs)) zuriak n = [' '|i<-[1..n]] --------------------------------- {- Main> loMasEco db [1113 GameBoy Advance 114.9 0 3 ,1116 PSP 154.9 2 4 ,2002 XBOX 360 PRO 419.0 2 1 ] -} -- 5 lista = zs where zs = (0,1) : zipWith fun [1..] zs fun i (x,y) = (x+i, y*i) {- Main> take 10 lista [(0,1),(1,1),(3,2),(6,6),(10,24),(15,120),(21,720),(28,5040),(36,40320),(45,362880)] -}