brendanzab / rusp

A minimal scripting and data language for Rust.
Apache License 2.0
16 stars 3 forks source link

Match expressions #11

Closed huonw closed 11 years ago

huonw commented 11 years ago

@bjz, r?

Rusp Repl
> (def foo (fn (a b)
+   (match a
+     (1 b)
+     ("bar" (+ b 1))
+     ((1 2) (+ b 2))
+     ((x .. y) y))))
()
> (foo 1 2)
2
> (foo 1 3)
3
> (foo "bar" 3)
4
> (foo (list 1 2) 3)
5
> (foo (list 3 2) 3)
(2)
> (foo (list 3 2 4 5) 3)
(2 4 5)
huonw commented 11 years ago

It doesn't do much validation of the patterns, beyond what would make them totally unusable (i.e. not fns or macros in a pattern), so something like (match ... ((a a) (+ 1 2)) ...) will run perfectly well, despite double-binding a.

brendanzab commented 11 years ago

Neat! Could we use this to implement fn matching?

huonw commented 11 years ago

That was the plan, although it may require some syntactic changes.