Advertisement
tomasfdel

Estructuras II TP 2 ArrSeq.hs

Jun 11th, 2018
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Seq
  2. import qualified Arr as A
  3. import Par
  4.  
  5. contraerS :: (a -> a -> a) -> A.Arr a -> Int -> A.Arr a
  6. contraerS f arr n = A.tabulate (\i -> if i == (div n 2) then arr A.! (2*i) else f (arr A.! (2*i)) (arr A.! (2*i + 1))) (div (n + 1) 2)
  7.  
  8.  
  9. instance Seq A.Arr where
  10.     lengthS = A.length
  11.     tabulateS = A.tabulate
  12.     nthS = (A.!)
  13.     fromList = A.fromList
  14.     joinS = A.flatten  
  15.  
  16.  
  17.     emptyS = fromList []
  18.    
  19.    
  20.     singletonS x = fromList [x]
  21.    
  22.  
  23.     takeS arr i = A.subArray 0 i arr
  24.    
  25.  
  26.     dropS arr i = A.subArray i ((lengthS arr) - i) arr
  27.    
  28.    
  29.     showtS arr = case lengthS arr of
  30.                       0 -> EMPTY
  31.                       1 -> ELT (nthS arr 0)
  32.                       n -> let mitad = div n 2
  33.                                (mitad1, mitad2) = (takeS arr mitad) ||| (dropS arr mitad)
  34.                            in NODE mitad1 mitad2
  35.    
  36.    
  37.     showlS arr = case lengthS arr of
  38.                       0 -> NIL
  39.                       n -> let (head, tail) = (nthS arr 0) ||| (dropS arr 1)
  40.                            in CONS head tail
  41.    
  42.      
  43.     appendS s t = joinS (fromList [s,t])
  44.    
  45.    
  46.     mapS f arr = tabulateS (\i -> f (nthS arr i)) (lengthS arr)
  47.    
  48.    
  49.     filterS f arr = joinS (tabulateS (\i -> if f (nthS arr i) then singletonS (nthS arr i) else emptyS) (lengthS arr))
  50.    
  51.    
  52.     reduceS f base arr = case lengthS arr of
  53.                                0 -> base
  54.                                1 -> f base (nthS arr 0)
  55.                                n -> reduceS f base (contraerS f arr n)
  56.    
  57.  
  58.     scanS f base arr = case lengthS arr of
  59.                             0 -> (singletonS base, base)
  60.                             1 -> (singletonS base, f base (nthS arr 0))
  61.                             n -> let arrC = if mod n 2 == 0 then tabulateS (\i -> f (nthS arr (2*i)) (nthS arr (2*i + 1))) (div n 2)
  62.                                                             else contraerS f arr n
  63.                                      (arrSeq, arrRed) = scanS f base arrC
  64.                                      expansion = tabulateS (\i -> if (mod i 2) == 0 then nthS arrSeq (div i 2) else f (nthS arrSeq (div i 2)) (nthS arr (i-1))) n
  65.                                  in (expansion, arrRed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement