Skip to content

Instantly share code, notes, and snippets.

@shugo
Created September 22, 2022 00:54
Show Gist options
  • Save shugo/03c1537cedebb78a08b3a42beaf4a65a to your computer and use it in GitHub Desktop.
Save shugo/03c1537cedebb78a08b3a42beaf4a65a to your computer and use it in GitHub Desktop.
def evaluate(expr)
case expr
in ["value", x]
x
in ["+", x, y]
evaluate(x) + evaluate(y)
in ["-", x, y]
evaluate(x) - evaluate(y)
in ["*", x, y]
evaluate(x) * evaluate(y)
in ["/", x, y]
evaluate(x) / evaluate(y)
end
end
def answer20(expr)
evaluate(expr)
end
p answer20(["+", ["value", 1], ["*", ["value", 2], ["value", 3]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment