Skip to content

Instantly share code, notes, and snippets.

@stettix
Created Jul 29, 2013
Embed
What would you like to do?
Pattern matching in eval()
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