diff options
Diffstat (limited to 'comparison/Haskell')
| -rw-r--r-- | comparison/Haskell/Makefile | 36 | ||||
| -rw-r--r-- | comparison/Haskell/ack3-11.hs | 9 | ||||
| -rw-r--r-- | comparison/Haskell/bsort-40000.hs | 38 | ||||
| -rw-r--r-- | comparison/Haskell/fib38.hs | 9 | ||||
| -rw-r--r-- | comparison/Haskell/isort-40000.hs | 36 | ||||
| -rw-r--r-- | comparison/Haskell/msort-800000.hs | 45 | ||||
| -rw-r--r-- | comparison/Haskell/qsort-800000.hs | 37 |
7 files changed, 210 insertions, 0 deletions
diff --git a/comparison/Haskell/Makefile b/comparison/Haskell/Makefile new file mode 100644 index 0000000..0042c28 --- /dev/null +++ b/comparison/Haskell/Makefile @@ -0,0 +1,36 @@ +CC = stack ghc +CFLAGS = -O2 +BENCHMARK = bash ../tools/sptest_commands.sh +TARGET = ack3-11 fib38 bsort-40000 isort-40000 qsort-800000 msort-800000 +#TARGET = ack3-11 +LOG = log.txt + + +.PHONY: all + +all: $(LOG) + +$(LOG): $(TARGET) + $(BENCHMARK) $(TARGET) + +ack3-11: ack3-11.hs + $(CC) -- $(CFLAGS) $< + +fib38: fib38.hs + $(CC) -- $(CFLAGS) $< + +bsort-40000: bsort-40000.hs + $(CC) -- $(CFLAGS) $< + +isort-40000: isort-40000.hs + $(CC) -- $(CFLAGS) $< + +qsort-800000: qsort-800000.hs + $(CC) -- $(CFLAGS) $< + +msort-800000: msort-800000.hs + $(CC) -- $(CFLAGS) $< + + +clean: + rm -f $(LOG) $(TARGET) *.o *.hi *~ diff --git a/comparison/Haskell/ack3-11.hs b/comparison/Haskell/ack3-11.hs new file mode 100644 index 0000000..7c8f350 --- /dev/null +++ b/comparison/Haskell/ack3-11.hs @@ -0,0 +1,9 @@ +ack 0 n = n+1 +ack m 0 = ack (m-1) 1 +ack m n = ack (m-1) (ack m (n-1)) + + +main = print $ (ack 3 11) + + + diff --git a/comparison/Haskell/bsort-40000.hs b/comparison/Haskell/bsort-40000.hs new file mode 100644 index 0000000..1940917 --- /dev/null +++ b/comparison/Haskell/bsort-40000.hs @@ -0,0 +1,38 @@ +{- +https://gist.github.com/masquerade0324/68f4f3fd3ab897fbe1ad +http://files.farka.eu/pub/AC21007/lec5.pdf +-} + +import System.Random + +bs xs 0 = xs +bs (x:y:ys) n = + if x<y then x:(bs (y:ys) (n-1)) + else y:(bs (x:ys) (n-1)) +bs xs n = xs + + +go xs 0 = xs +go xs n = go (bs xs n) (n-1) + +bsort xs = go xs (length xs -1) + +{- +main = print $ bsort [3,2,8,5] +-} + + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 40000 + let sorted = bsort list + print (take 10 sorted) diff --git a/comparison/Haskell/fib38.hs b/comparison/Haskell/fib38.hs new file mode 100644 index 0000000..96040eb --- /dev/null +++ b/comparison/Haskell/fib38.hs @@ -0,0 +1,9 @@ +fib 0 = 0 +fib 1 = 1 +fib n = fib (n - 2) + fib (n - 1) + + +main = print $ fib 38 + + + diff --git a/comparison/Haskell/isort-40000.hs b/comparison/Haskell/isort-40000.hs new file mode 100644 index 0000000..f834d00 --- /dev/null +++ b/comparison/Haskell/isort-40000.hs @@ -0,0 +1,36 @@ +{- +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +-} +import System.Random + +isort [] = [] +isort (x:xs) = insert x (isort xs) + +insert x [] = [x] +insert x (y:ys) = + if x<=y then x:y:ys + else y:(insert x ys) + + +{- +main = print $ isort [3,2,8,5] +-} + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 40000 + let sorted = isort list + print (take 10 sorted) + + + + diff --git a/comparison/Haskell/msort-800000.hs b/comparison/Haskell/msort-800000.hs new file mode 100644 index 0000000..eb1a24d --- /dev/null +++ b/comparison/Haskell/msort-800000.hs @@ -0,0 +1,45 @@ +{- +https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml +-} +import System.Random + +mysplit [] = ([], []) +mysplit [a] = ([a], []) +mysplit (a:b:cs) = (a:m, b:n) + where + (m,n) = mysplit cs + +merge ([], ys) = ys +merge (xs, []) = xs +merge (x:xs, y:ys) + | x<y = x:merge(xs, y:ys) + | otherwise = y:merge(x:xs, ys) + +msort [] = [] +msort [a] = [a] +msort [a,b] + | a<b = [a,b] + | otherwise = [b,a] +msort xs = merge (msort m, msort n) + where + (m,n) = mysplit xs + +{- +main = print $ msort [3,2,8,5] +-} + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 800000 + let sorted = msort list + print (take 10 sorted) + + diff --git a/comparison/Haskell/qsort-800000.hs b/comparison/Haskell/qsort-800000.hs new file mode 100644 index 0000000..492efef --- /dev/null +++ b/comparison/Haskell/qsort-800000.hs @@ -0,0 +1,37 @@ +{- +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +-} +import System.Random + + +qsort [] = [] +qsort [x] = [x] +qsort (x:xs) = (qsort left) ++ [x] ++ (qsort right) + where (left, right) = part x xs + +part pivot [] = ([], []) +part pivot (x:xs) + | x<pivot = (x:below, above) + | otherwise = (below, x:above) + where + (below, above) = part pivot xs + + +{- +main = print $ qsort [3,2,8,5] +-} + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 800000 + let sorted = qsort list + print (take 10 sorted) + |
