Created
July 29, 2013 16:41
-
-
Save stettix/6105671 to your computer and use it in GitHub Desktop.
Pattern matching in eval()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eval(expr: Expr, env: Env): Expr = expr match { | |
// ... | |
case "cons" :: arg1 :: arg2 :: Nil => eval(arg2, env) match { | |
case l: List[Expr] => eval(arg1, env) :: l | |
case arg => throw new IllegalArgumentException(s"Second argument to 'cons' should be list, was: '$arg'") | |
} | |
case "cons" :: args => throw new IllegalArgumentException(s"Invalid arguments to 'cons': $args") | |
case "null?" :: head :: Nil => eval(head, env) == List() | |
case "null?" :: _ => throw new IllegalArgumentException("Expected exactly one argument for 'null?'") | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment