Advertisement
tomasfdel

Estructuras II TP 2 ListSeq.hs

Jun 5th, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Par
  2. import Seq
  3.  
  4. tabulateSaux :: (Int -> a) -> Int -> Int -> [a]
  5. tabulateSaux f nat valor = if valor == nat
  6.                               then []
  7.                               else let (tx, txs) = (f valor) ||| (tabulateSaux f nat (valor + 1))
  8.                                    in tx : txs
  9.  
  10. contraerS :: (a -> a -> a) -> [a] -> [a]
  11. contraerS f [] = []
  12. contraerS f [x] = [x]
  13. contraerS f (x:y:xs) = let (xyC, xsC) = (f x y) ||| (contraerS f xs)
  14.                        in xyC : xsC
  15.  
  16. expandirS :: (a -> a -> a) -> [a] -> [a] -> Int -> [a]
  17. expandirS f [] [] idx = []
  18. expandirS f [x] [x'] idx = [x']
  19. expandirS f s@(x:y:xs) s'@(x':xs') idx = if (mod idx 2) == 0
  20.                                             then x' : (expandirS f s s' (idx + 1))
  21.                                             else let (head, tail) = (f x' x) ||| (expandirS f xs xs' (idx + 1))
  22.                                                  in head : tail
  23.  
  24. instance Seq [] where
  25.    emptyS = []
  26.  
  27.  
  28.    singletonS x = [x]
  29.  
  30.  
  31.    lengthS [] = 0
  32.    lengthS (x:xs) = 1 + lengthS xs
  33.  
  34.  
  35.    -- Suponemos nos dan un índice válido.
  36.    nthS (x:xs) 0 = x
  37.    nthS (x:xs) n = nthS xs (n - 1)
  38.  
  39.  
  40.    mapS f [] = []
  41.    mapS f (x:xs) = let (mx, mxs) = (f x) ||| (mapS f xs) in mx : mxs
  42.  
  43.  
  44.    tabulateS f nat = tabulateSaux f nat 0
  45.  
  46.  
  47.    filterS f [] = []
  48.    filterS f (x:xs) = let (fx, fxs) = (f x) ||| (filterS f xs) in
  49.                       if fx then x : fxs else fxs
  50.  
  51.  
  52.    appendS = (++)
  53.  
  54.  
  55.    takeS [] _ = []
  56.    takeS xs 0 = []
  57.    takeS (x:xs) n = x : (takeS xs (n - 1))
  58.  
  59.  
  60.    dropS [] _ = []
  61.    dropS xs 0 = xs
  62.    dropS (x:xs) n = dropS xs (n - 1)
  63.  
  64.  
  65.    showtS [] = EMPTY
  66.    showtS [x] = ELT x
  67.    showtS xs = let mitad = div (lengthS xs) 2
  68.                    (mitad1, mitad2) = (takeS xs mitad) ||| (dropS xs mitad)
  69.                in NODE mitad1 mitad2
  70.  
  71.  
  72.    showlS [] = NIL
  73.    showlS (x:xs) = CONS x xs
  74.  
  75.  
  76.    joinS xs = [elem | listaElem <- xs, elem <- listaElem]
  77.  
  78.  
  79.    reduceS f base [] = base
  80.    reduceS f base [x] = f base x
  81.    reduceS f base xs = reduceS f base (contraerS f xs)
  82.  
  83.  
  84.    scanS f base [] = (singletonS base, base)
  85.    scanS f base [x] = (singletonS base, f base x)
  86.    scanS f base xs = let xsC = contraerS f xs
  87.                          (xsSeq, xsRed) = scanS f base xsC
  88.                      in (expandirS f xs xsSeq 0, xsRed)
  89.    
  90.    fromList xs = xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement