Smalltalk-ish "yourself" and primitive pattern matching that really isn't

Jeremy Tregunna <[email protected]>
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
Alright, so I'll cut right to the chase today.

Today I was playing around trying to design a syntax that would extend Io in such a way that we can more easily catch errors like missing )s. So I've been playing around with a few syntaxes, and I got to writing a fibonacci as a way of looking at the syntaxes that I was building. A couple are shown below.

    fib: n ->
      n match: ->
        < 2 -> 1
        _   -> fib(n - 1) + fib(n - 2)

and

    fib(n):
      n match:
        < 2: 1
        _  : fib(n - 1) + fib(n - 2)

So an obvious theme here is significant whitespace. Let's ignore the discussions on that topic for another day. The reason I post that is because of the match method. I didn't even think about writing it another way, just jotted it down like this. So what's it do? Well, it applies the message on the left hand side of the -> or : and if that evaluates to a truth value, evaluates the right hand side and returns its result.

Simple conditional structure, not really pattern matching, but anyway. An implementation is presented in this gist: https://gist.github.com/1343169.

One of the things that I noticed when I wrote this is, that without a smalltalk-ish ';' (yourself) operator, it was really ugly to write; having to chain match()s. What I really wanted was the ability to write something like:

    n;
      match(< 2, 1);
      match(true, fib(n - 1) + fib(n - 2))

and get the right result. So I implemented a Smalltalk-ish "yourself" method. It works kinda like `do` except it's dynamically scoped, and returns when a truth value is hit with its result; otherwise returns self.

Anyway, sharing it in case someone finds it useful.

Regards,

Jeremy Tregunna
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.