Permalink
Browse files

Haskell code optimisations

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@738 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information...
grammarware committed Jan 17, 2010
1 parent cbd2c0e commit 9d7d7a6ec5d57a5c5e62cca1f0b0c0a975c62647
@@ -1,4 +1,4 @@
test:
ghci -ihutton:. parseB.hs
runhaskell -ihutton:. parseB.hs
@@ -1,5 +1,5 @@
test:
ghci foldB.hs
runhaskell foldB.hs
clean:
rm -f *~
@@ -12,7 +12,7 @@ depth :: B -> Int
depth = foldB
1
1
(\ x y z -> 1 + (maximum [x, y, z]))
(\ x y z -> 1 + maximum [x, y, z])
countT :: B -> Int
countT = foldB
@@ -32,6 +32,6 @@ eval = foldB
False
(\ x y z -> if x then y else z)
main = do
print (eval (IfB (IfB TrueB FalseB FalseB) FalseB FalseB))
main =
print (eval (IfB (IfB TrueB FalseB FalseB) FalseB FalseB))
@@ -18,13 +18,13 @@ init1 :: [a] -> [a]
init1 = reverse . tail . reverse
init2 :: [a] -> [a]
init2 = reverse . (drop 1) . reverse
init2 = reverse . drop 1 . reverse
init3 :: [a] -> [a]
init3 xs = take (length xs - 1) xs
init4 :: [a] -> [a]
init4 xs = if length xs == 1 then [] else [head xs] ++ init4 (tail xs)
init4 xs = if length xs == 1 then [] else head xs : init4 (tail xs)
---- Exercises block 3, slides 315–316, (2)
-- take an array, return True if it's a palindrome
@@ -34,7 +34,7 @@ palindrome xs = reverse xs == xs
---- Exercises block 5, slides 360–362
-- define scalar product of two arrrays
scalar1 :: [Int] -> [Int] -> Int
scalar1 xs ys = sum (zipWith (*) xs ys)
scalar1 xs = sum . zipWith (*) xs
scalar2 :: [Int] -> [Int] -> Int
scalar2 xs ys | length xs == length ys = sum [(xs !! i)*(ys !! i) | i <- [0..(length xs - 1)]]
@@ -1,5 +1,5 @@
test:
ghci -ihutton:. Test.hs
runhaskell -ihutton:. Test.hs
clean:
rm -f *~ *.hi *.o
@@ -7,11 +7,11 @@ import NB
main = do
print $ eval $ fst $ head $ parse nb "0"
print $ evaln $ fst $ head $ parse nb "0"
print $ evalnb $ fst $ head $ parse nb "0"
print $ eval $ fst $ head $ parse nb "if true then false else if false then true else false fi fi"
print $ evaln $ fst $ head $ parse nb "if true then false else if false then true else false fi fi"
print $ evalnb $ fst $ head $ parse nb "if true then false else if false then true else false fi fi"
print $ eval $ fst $ head $ parse nb "if is pred(succ(0)) zero? then true else false fi"
print $ evaln $ fst $ head $ parse nb "if is pred(succ(0)) zero? then true else false fi"
print $ evalnb $ fst $ head $ parse nb "if is pred(succ(0)) zero? then true else false fi"
print $ eval $ fst $ head $ parse nb "succ(succ(succ(pred(succ(0)))))"
print $ evaln $ fst $ head $ parse nb "succ(succ(succ(pred(succ(0)))))"
print $ evalnb $ fst $ head $ parse nb "succ(succ(succ(pred(succ(0)))))"
@@ -1,3 +1,5 @@
import Data.Maybe
-- assignment 5, part 1
{-
lookup(M,X,Y) :- append(_,[(X,Y)|_],M).
@@ -67,10 +69,10 @@ evals Skip e = e
evala :: AExpression -> LookupTable -> Int
evala (Number n) _ = n
evala (Identifier i) e = maybe (error "Undefined variable") id (mylookup i e)
evala (Add a1 a2) e = (evala a1 e) + (evala a2 e)
evala (Sub a1 a2) e = (evala a1 e) - (evala a2 e)
evala (Mul a1 a2) e = (evala a1 e) * (evala a2 e)
evala (Identifier i) e = Data.Maybe.fromMaybe (error "Undefined variable") (mylookup i e)
evala (Add a1 a2) e = evala a1 e + evala a2 e
evala (Sub a1 a2) e = evala a1 e - evala a2 e
evala (Mul a1 a2) e = evala a1 e * evala a2 e
-- evalb :: BExpression -> LookupTable -> Bool
--- evalb ... = ...
@@ -1,6 +1,5 @@
all:
ghc while.hs
./a.out
runhaskell while.hs
test:
ghci while.hs
@@ -1,5 +1,7 @@
module Main where
import Data.Maybe
-- assignment 5, part 1
{-
lookup(M,X,Y) :- append(_,[(X,Y)|_],M).
@@ -15,8 +17,8 @@ myupdate :: LookupTable -> Identifier -> Int -> LookupTable
myupdate [] x v = [(x, v)]
myupdate ((x1,v1):xvs) x2 v2 =
if x1 == x2
then ((x1,v2):xvs)
else ((x1,v1):(myupdate xvs x2 v2))
then (x1,v2):xvs
else (x1,v1):myupdate xvs x2 v2
-- assignment 5, part 2
data Statement
@@ -73,19 +75,18 @@ evals (While b s) e = if evalb b e then evals (While b s) (evals s e) else e
evala :: AExpression -> LookupTable -> Int
evala (Number n) _ = n
evala (Identifier i) e = maybe (error "Undefined variable") id (mylookup i e)
evala (Add a1 a2) e = (evala a1 e) + (evala a2 e)
evala (Sub a1 a2) e = (evala a1 e) - (evala a2 e)
evala (Mul a1 a2) e = (evala a1 e) * (evala a2 e)
evala (Identifier i) e = Data.Maybe.fromMaybe (error "Undefined variable") (mylookup i e)
evala (Add a1 a2) e = evala a1 e + evala a2 e
evala (Sub a1 a2) e = evala a1 e - evala a2 e
evala (Mul a1 a2) e = evala a1 e * evala a2 e
evalb :: BExpression -> LookupTable -> Bool
evalb BTrue _ = True
evalb BFalse _ = False
evalb (Equals a1 a2) e = (evala a1 e) == (evala a2 e)
evalb (LessThanOrEqual a1 a2) e = (evala a1 e) <= (evala a2 e)
evalb (Equals a1 a2) e = evala a1 e == evala a2 e
evalb (LessThanOrEqual a1 a2) e = evala a1 e <= evala a2 e
evalb (Not b) e = not (evalb b e)
evalb (And b1 b2) e = (evalb b1 e) && (evalb b2 e)
evalb (And b1 b2) e = evalb b1 e && evalb b2 e
main =
do
print (evals test [])
print (evals test [])

0 comments on commit 9d7d7a6

Please sign in to comment.