aboutsummaryrefslogtreecommitdiff
path: root/haskell/Core.hs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-12-23 23:49:23 -0700
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:53 -0600
commit2988d38e84bce8531c0f21fafecb7483593cda73 (patch)
tree4aad5f8d6cca8a897eb177dd3ec16b7301cb2745 /haskell/Core.hs
parentfa9a9758e0d15abe670fbbfd8efa1fce013b1414 (diff)
downloadmal-2988d38e84bce8531c0f21fafecb7483593cda73.tar.gz
mal-2988d38e84bce8531c0f21fafecb7483593cda73.zip
Haskell: add step7 and 8.
Diffstat (limited to 'haskell/Core.hs')
-rw-r--r--haskell/Core.hs34
1 files changed, 34 insertions, 0 deletions
diff --git a/haskell/Core.hs b/haskell/Core.hs
index 9849ddb..0116d55 100644
--- a/haskell/Core.hs
+++ b/haskell/Core.hs
@@ -20,6 +20,12 @@ run_1 f args = do
(x:[]) -> return $ f x
_ -> error $ "function takes a single argument"
+run_2 :: (MalVal -> MalVal -> MalVal) -> [MalVal] -> IO MalVal
+run_2 f args = do
+ case args of
+ (x:y:[]) -> return $ f x y
+ _ -> error $ "function takes a two arguments"
+
-- String functions
@@ -73,6 +79,29 @@ hash_map args = do
-- Sequence functions
+cons x Nil = MalList [x]
+cons x (MalList lst) = MalList $ x:lst
+cons x (MalVector lst) = MalList $ x:lst
+
+concat1 a (MalList lst) = a ++ lst
+concat1 a (MalVector lst) = a ++ lst
+do_concat args = return $ MalList $ foldl concat1 [] args
+
+nth args = do
+ case args of
+ (MalList lst):(MalNumber idx):[] ->
+ if idx < length lst then return $ lst !! idx
+ else error "nth: index out of range"
+ (MalVector lst):(MalNumber idx):[] ->
+ if idx < length lst then return $ lst !! idx
+ else error "nth: index out of range"
+
+first (MalList lst) = if length lst > 0 then lst !! 0 else Nil
+first (MalVector lst) = if length lst > 0 then lst !! 0 else Nil
+
+rest (MalList lst) = MalList $ drop 1 lst
+rest (MalVector lst) = MalList $ drop 1 lst
+
empty_Q Nil = MalTrue
empty_Q (MalList []) = MalTrue
empty_Q (MalVector []) = MalTrue
@@ -109,5 +138,10 @@ ns = [
("hash-map", _func $ hash_map),
("map?", _func $ run_1 $ _hash_map_Q),
+ ("cons", _func $ run_2 $ cons),
+ ("concat", _func $ do_concat),
+ ("nth", _func nth),
+ ("first", _func $ run_1 $ first),
+ ("rest", _func $ run_1 $ rest),
("empty?", _func $ run_1 $ empty_Q) ,
("count", _func $ run_1 $ count)]