Skip to content

Instantly share code, notes, and snippets.

@stettix
Created July 29, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stettix/6105671 to your computer and use it in GitHub Desktop.
Save stettix/6105671 to your computer and use it in GitHub Desktop.
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