Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 1)
- type Color = (Float, Float, Float)
- mezclar :: Color -> Color -> Color
- mezclar (r1, g1, b1) (r2, g2, b2) = ( (r1 + r2)/2 , (g1 + g2)/2, (b1 + b2)/2 )
- -- 2)
- type Linea = (Int, [Char])
- vacia :: Linea
- vacia = (0, [])
- moverIzq :: Linea -> Linea
- moverIzq (cursor, linea) = if cursor == 0 then (0, linea) else (cursor - 1, linea)
- moverDer :: Linea -> Linea
- moverDer (cursor, linea) = if cursor == length(linea) then (cursor, linea) else (cursor + 1, linea)
- moverIni :: Linea -> Linea
- moverIni (cursor, linea) = (0, linea)
- moverFin :: Linea -> Linea
- moverFin (cursor, linea) = (length linea, linea)
- insertar :: Char -> Linea -> Linea
- insertar caracter (cursor, linea) = (cursor, (take cursor linea)++ [caracter] ++ (drop cursor linea))
- borrar :: Linea -> Linea
- borrar (0, linea) = (0, linea)
- borrar (cursor, linea) = (cursor - 1, (take (cursor - 1) linea) ++ (drop cursor linea))
- -- 3)
- data CList a = EmptyCL | CUnit a | Consnoc a (CList a) a
- headCL :: CList a -> a
- headCL (CUnit v) = v
- headCL (Consnoc v1 xs v2) = v1
- rearmar :: CList a -> a -> CList a
- rearmar xs v = case xs of
- EmptyCL -> (CUnit v)
- (CUnit v1) -> (Consnoc v1 EmptyCL v)
- (Consnoc v1 ys v2) -> (Consnoc v1 (rearmar ys v2) v)
- rearmar2 :: a -> CList a -> CList a
- rearmar2 v xs = case xs of
- EmptyCL -> (CUnit v)
- (CUnit v1) -> (Consnoc v EmptyCL v1)
- (Consnoc v1 ys v2) -> (Consnoc v (rearmar2 v1 ys) v2)
- tailCL :: CList a -> CList a
- tailCL (CUnit v) = EmptyCL
- tailCL (Consnoc v1 xs v2) = rearmar xs v2
- isEmptyCL :: CList a -> Bool
- isEmptyCL EmptyCL = True
- isEmptyCL _ = False
- isCUnitCL :: CList a -> Bool
- isCUnitCL (CUnit v) = True
- isCUnitCL _ = False
- data CList a = EmptyCL | CUnit a | Consnoc a (CList a) a deriving Show
- headCL :: CList a -> a
- headCL (CUnit v) = v
- headCL (Consnoc v1 xs v2) = v1
- tailCL :: CList a -> CList a
- tailCL (CUnit v) = EmptyCL
- tailCL (Consnoc v1 xs v2) = snocCL xs v2
- reverseCL :: CList a -> CList a
- reverseCL EmptyCL = EmptyCL
- reverseCL (CUnit v) = (CUnit v)
- reverseCL (Consnoc v1 xs v2) = Consnoc v2 (reverseCL xs) v1
- consCL :: a -> CList a -> CList a
- consCL x EmptyCL = CUnit x
- consCL x (CUnit y) = Consnoc x EmptyCL y
- consCL x (Consnoc y1 z y2) = Consnoc x (consCL y1 z) y2
- snocCL :: CList a -> a -> CList a
- snocCL EmptyCL x = CUnit x
- snocCL (CUnit y) x = Consnoc y EmptyCL x
- snocCL (Consnoc y1 z y2) x = Consnoc y1 (snocCL z y2) x
- initsAux :: CList a -> [CList a]
- initsAux EmptyCL = [EmptyCL]
- initsAux lista = EmptyCL:(map (consCL (headCL lista)) (initsAux (tailCL lista)) )
- listToCL :: [a] -> CList a
- listToCL [] = EmptyCL
- listToCL (x:xs) = consCL x (listToCL xs)
- inits :: CList a -> CList (CList a)
- inits lista = listToCL (initsAux lista)
- lasts:: CList a -> CList (CList a)
- lasts lista = listToCL (map reverseCL (initsAux (reverseCL lista)))
- concatDosCL :: CList a -> CList a -> CList a
- concatDosCL l1 EmptyCL = l1
- concatDosCL EmptyCL l2 = l2
- concatDosCL (CUnit v1) (CUnit v2) = Consnoc v1 EmptyCL v2
- concatDosCL (Consnoc v11 xs v12) (CUnit v2) = Consnoc v11 (rearmar xs v12) v2
- concatDosCL (CUnit v1) (Consnoc v21 xs v22) = Consnoc v1 (rearmar2 v21 xs) v22
- concatDosCL (Consnoc v11 xs v12) (Consnoc v21 ys v22) = Consnoc v11 (concatDosCL (rearmar xs v12) (rearmar2 v21 ys)) v22
- concatCL :: CList (CList a) -> CList a
- concatCL EmptyCL = EmptyCL
- concatCL (CUnit l) = l
- concatCL (Consnoc l1 xs l2) = (concatDosCL((concatDosCL l1 (concatCL xs))) l2)
- -- 4)
- import Prelude
- data Aexp = Num Int | Prod Aexp Aexp | Div Aexp Aexp
- --data Maybe a = Nothing | Just a
- eval :: Aexp -> Int
- eval (Num n) = n
- eval (Prod n1 n2) = (eval n1) * (eval n2)
- eval (Div n1 n2) = div (eval n1) (eval n2)
- seval :: Aexp -> Maybe Int
- seval (Num n) = Just n
- seval (Prod n1 n2) = case ((seval n1), (seval n2)) of
- (Nothing, v2) -> Nothing
- (v1, Nothing) -> Nothing
- ((Just v1), (Just v2)) -> Just (v1*v2)
- seval (Div n1 n2) = case ((seval n1), (seval n2)) of
- (Nothing, v2) -> Nothing
- (v1, Nothing) -> Nothing
- ((Just v1), (Just v2)) -> if v2 == 0 then Nothing else Just (div v1 v2)
- -- 5)
- data Tree a = Hoja | Nodo (Tree a) a (Tree a) deriving Show
- completo :: a -> Int -> Tree a
- completo dato 0 = Hoja
- completo dato n = let subarbol = (completo dato (n - 1))
- in (Nodo subarbol dato subarbol)
- balanceado :: a -> Int -> Tree a
- balanceado dato 0 = Hoja
- balanceado dato 1 = Nodo Hoja dato Hoja
- balanceado dato n = if (mod (n - 1) 2) == 0 then let subarbol = (balanceado dato (div n 2))
- in (Nodo subarbol dato subarbol)
- else let subI = (balanceado dato (div n 2))
- subD = (balanceado dato ((div n 2) - 1))
- in (Nodo subI dato subD)
- -- 6)
- data GenTree a = EmptyG | NodeG a [GenTree a]
- data BinTree a = EmptyB | NodeB (BinTree a) a (BinTree a)
- checkBT :: Eq a => BinTree a -> BinTree a -> Bool
- checkBT EmptyB EmptyB = True
- checkBT (NodeB l1 v1 r1) EmptyB = False
- checkBT EmptyB (NodeB l2 v2 r2) = False
- checkBT (NodeB l1 v1 r1) (NodeB l2 v2 r2) = (v1 == v2) && (checkBT l1 l2) && (checkBT r1 r2)
- g2btAux :: GenTree a -> [GenTree a] -> BinTree a
- g2btAux (NodeG v []) [] = (NodeB EmptyB v EmptyB)
- g2btAux (NodeG v []) (y:ys) = (NodeB EmptyB v (g2btAux y ys) )
- g2btAux (NodeG v (x:xs)) [] = (NodeB (g2btAux x xs) v EmptyB)
- g2btAux (NodeG v (x:xs)) (y:ys) = (NodeB (g2btAux x xs) v (g2btAux y ys))
- g2bt:: GenTree a -> BinTree a
- g2bt EmptyG = EmptyB
- g2bt (NodeG x lista) = g2btAux (NodeG x lista) []
- test1 = (NodeB (NodeB (NodeB EmptyB 'M' (NodeB EmptyB 'N' EmptyB)) 'G' (NodeB EmptyB 'H' (NodeB EmptyB 'I' EmptyB))) 'B' EmptyB)
- test2 = (NodeG 'B' [(NodeG 'G' [(NodeG 'M' []), (NodeG 'N' [])]), (NodeG 'H' []), (NodeG 'I' []) ])
- -- 7 y 8)
- data Bin a = Hoja | Nodo (Bin a) a (Bin a)
- minimumBST :: Bin a -> a
- minimumBST (Nodo Hoja a r) = a
- minimumBST (Nodo l a r) = minimumBST l
- maximumBST :: Bin a -> a
- maximumBST (Nodo l a Hoja) = a
- maximumBST (Nodo l a r) = maximumBST r
- checkBST :: Ord a => Bin a -> Bool
- checkBST Hoja = True
- checkBST (Nodo Hoja a Hoja) = True
- checkBST (Nodo l a Hoja) = (a >= (maximumBST l)) && (checkBST l)
- checkBST (Nodo Hoja a r) = (a <= (minimumBST r)) && (checkBST r)
- checkBST (Nodo l a r) = (a <= (maximumBST l)) && (a <= (minimumBST r)) && (checkBST l) && (checkBST r)
- memberAux :: (Num a, Ord a) => a -> Bin a -> a -> Bool
- memberAux valor Hoja candidato = valor == candidato
- memberAux valor (Nodo l a r) candidato = if valor <= a then (memberAux valor l a) else (memberAux valor r candidato)
- member :: (Num a, Ord a) => a -> Bin a -> Bool
- member valor arbol = memberAux valor arbol (valor + 1)
- -- 9)
- data Color = R | B
- data RBT a = E | T Color (RBT a) a (RBT a)
- makeBlack :: RBT a -> RBT a
- makeBlack E = E
- makeBlack (T _ l x r) = T B l x r
- lbalance :: Color -> RBT a -> a -> RBT a -> RBT a
- lbalance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
- lbalance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
- lbalance c l a r = T c l a r
- rbalance :: Color -> RBT a -> a -> RBT a -> RBT a
- rbalance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
- rbalance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
- rbalance c l a r = T c l a r
- insert :: Ord a => a -> RBT a -> RBT a
- insert x t = makeBlack (ins x t)
- where ins x E = T R E x E
- ins x (T c l y r) | x < y = lbalance c (ins x l) y r
- | x > y = rbalance c l y (ins x r)
- | otherwise = T c l y r
- -- 10)
- type Rank = Int
- data Heap a = E | N Rank a (Heap a) (Heap a)
- rank :: Heap a -> Rank
- rank E = 0
- rank (N r _ _ _) = r
- makeH:: a -> Heap a -> Heap a -> Heap a
- makeH x a b = if rank a >= rank b then N (rank b + 1) x a b
- else N (rank a + 1) x b a
- merge :: Ord a => Heap a -> Heap a -> Heap a
- merge h1 E = h1
- merge E h2 = h2
- merge h1@(N _ x a1 b1) h2@(N _ y a2 b2) =
- if x <= y then makeH x a1 (merge b1 h2)
- else makeH y a2 (merge h1 b2)
- fromHeapList :: Ord a => [Heap a] -> [Heap a]
- fromHeapList [] = []
- fromHeapList [x] = [x]
- fromHeapList (x:y:xs) = fromHeapList ( (merge x y) : (fromHeapList xs) )
- fromList :: Ord a => [a] -> Heap a
- fromList [] = E
- fromList xs = let [x] = fromHeapList (map (\x -> N 1 x E E) xs) in x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement