Arc Forumnew | comments | leaders | submitlogin
1 point by Pauan 4434 days ago | link | parent

Oh yeah, I mentioned pattern matching is customizable... it's actually really really awesome. First, you've got this simple function "pattern-match":

https://github.com/Pauan/nulan/blob/f3f5c3550273bd757ca2d7f3...

How it works is... it's passed three arguments: the environment, the pattern, and the value. It returns a new environment which is the result of matching the pattern to the value.

But it doesn't do an awful lot: it handles symbols, the wildcard ~, lists use the %pattern-match function of an object, and everything else just uses "eq?".

So, for instance, the "list" pattern matching is implemented here:

https://github.com/Pauan/nulan/blob/f3f5c3550273bd757ca2d7f3...

It's actually a lot simpler than it looks. It just recurses down the pattern and value, calling "pattern-match" on each element of the lists. Then, at the end, if either the pattern or value is not null, there was a mismatch, so it throws an error.

The argument list of a vau obviously uses the "pattern-match" function, but another place it's used is "def":

https://github.com/Pauan/nulan/blob/f3f5c3550273bd757ca2d7f3...

See how simple that is? It simply unboxes the dynamic environment, runs it through the pattern matcher, then assigns it to the dynamic environment[1].

With that small amount of code, the following now works:

  (def (list a b) (list 1 2))
Which will bind "a" to 1 and "b" to 2. And as said, it's completely customizable: just slap a %pattern-match property onto any object.

And because Nulan is a vau-based language with first-class environments, you can use "def" inside vaus to create local bindings:

  (fn ()
    (def a 5)
    a)
This is like "var" in JavaScript only much better.

---

* [1]: The reason the Racket version is a bit funky is because the "pattern-match" function is written in Nulan style. Here's how "def" would be written in Nulan:

  (def def
    (vau e {n v}
      (let v (eval e v)
        (set! e (pattern-match (unbox e) n v))
        v)))


1 point by Pauan 4434 days ago | link

I just added in dictionary pattern matching:

https://github.com/Pauan/nulan/blob/95a73a115fdef0cb27fae2e7...

See how easy that is? It took me maybe 5-10 minutes to add it in.

And it naturally supports recursion as deeply nested as you want:

  (def (dict "foo" (dict "bar" b) "qux" c)
       (dict "foo" (dict "bar" 1) "qux" 2))
Now the variables "b" is 1 and "c" is 2.

-----