Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lisp in Huskell

αλεx π
December 20, 2013
110

Lisp in Huskell

αλεx π

December 20, 2013
Tweet

Transcript

  1. (def b 100) ;; defining vars! (def a (fn [c]

    (+ b c))) ;; defining functions! (a 5 7) ;; executing functions
  2. data LispExpression = LispSymbol String |! ReservedKeyword ReservedKeyword |! LispList

    [LispExpression] |! LispVector [LispExpression] |! LispNumber Integer |! --LispNumber LispNum |! LispString String |! LispBool Bool |! LispFunction LispFunk |! -- LispFunction [LispExpression] LispExpression |! LispNil! deriving (Generic)
  3. Reserved keywords (if, def, fn) parseReserved :: Parser LispExpression! parseReserved

    = do! res <- try (string "def") <|>! try (string "if") <|>! try (string "fn")! return $ toSexp res
  4. Numbers parseNumber :: Parser LispExpression! parseNumber = do! _ <-

    try (many (string "#d"))! sign <- many (oneOf "-")! num <- many1 (digit)! if (length sign) > 1! then pzero! else return $ (LispNumber . read) $ sign ++ num
  5. Booleans parseTrue :: Parser LispExpression! parseTrue = do! res <-

    try (string "true")! return $ LispBool True! ! parseFalse :: Parser LispExpression! parseFalse = do! res <- try (string "false")! return $ LispBool False
  6. Booleans parseReserved :: Parser LispExpression! parseReserved = do! res <-

    try (string "def") <|>! try (string "if") <|>! try (string "fn")! return $ toSexp res! ! parseTrue :: Parser LispExpression! parseTrue = do! res <- try (string "true")! return $ LispBool True
  7. Symbols parseSymbol :: Parser LispExpression! parseSymbol = do! first <-

    letter <|> symbols! rest <- many (letter <|> digit <|> symbols)! let symbol = first:rest! return $ LispSymbol symbol
  8. Lists parens parseList <|>! ! ! ! parseList :: Parser

    LispExpression! parseList = liftM LispList $ sepBy parseLispExpression whiteSpace
  9. Lists parseString :: Parser LispExpression! parseString = do _ <-

    char '"'! x <- many (noneOf "\"")! _ <- char '"'! return $ LispString x
  10. (def a 1)! (def b 2)! (def c 3)! (def

    d 4)! ! (+ a b (* c d))
  11. (def a 1)! (def b 2)! (def c 3)! (def

    d 4)! ! (+ a b (* 4 3))
  12. type Environment k v = H.BasicHashTable k v! type LispEnvironment

    = (Environment LispExpression LispExpression)!
  13. findVar :: LispEnvironment -> LispExpression -> ! ! ! !

    ! ! IO (Maybe LispExpression)! ! findVar env symbol = H.lookup env symbol!
  14. (def sum ! (fn [a b] ! (fn [c] (+

    c a b))))! ! (+ a b) ;; Shouldn't work Creates Closure
  15. (def sum ! (fn [a b] ! (fn [c] (+

    c a b))))! ! (+ a b) ;; Shouldn't work Limited Visibility
  16. (def sum ! (fn [a b] ! (fn [c] (+

    c a b))))! ! (+ a b) ;; Shouldn't work Limited Visibility
  17. numericOp :: (Integer -> Integer -> Integer) -> ! [LispExpression]

    -> LispExpression! ! numericOp op args = LispNumber $ foldl1 op $ ! ! ! ! ! ! ! ! ! map unpackNum args! ! builtInOp :: String -> [LispExpression] -> ! LispExpression! builtInOp "+" args = numericOp (+) args
  18. eval env closure (LispList[(ReservedKeyword IfKeyword), ! ! ! ! !

    ! ! ! ! testExpression,! truthyExpression, falsyExpression]) = do! test <- (eval env closure testExpression)! res <- if (isTrue test)! then (eval env closure truthyExpression)! else (eval env closure falsyExpression)! return res
  19. (def map! (fn [f coll]! (if (empty? coll)! (quote ())!

    (cons (f (first coll))! (map f (next coll)))))) Recur here
  20. (def reduce! (fn [f coll acc]! (if (empty? coll)! acc!

    (reduce f (next coll) (f acc (first coll)))))) Recur here