Rubys Upto in a Scheme macro

For a lot of folks, Ruby has a syntax that is really irresistible. Iteration is one of the things in particular that is described as being quite simply, fun. Here is how it works:

Pass a message to the integer start index which includes both the inclusive stop index and a single argument function that is to be called with the current index in the iteration. The implementation of this feature, which can be found here, is written nearly as you would imagine, albeit in C. Here is how the code looks when you call this function:

1.upto(3) {|x| printf "#{x}\n"}
1
2
3

With my programmer’s glasses on this, I would say that this syntax feels nice. In fact, it feels nice enough to think about adding a feature like this to Scheme. Here is what I came up with:

(define-syntax from
  (syntax-rules (upto)
    ((from x upto y fn)
     (let ([finish (add1 y)])
       (let loop ([cur x])
         (if (not (= cur finish))
             (begin
               (fn cur)
               (loop (add1 cur)))))))))

Here is how the Ruby sample from above looks in Scheme.

(from 1 upto 3 (λ (x) (printf "~a~n" x)))
1
2
3

What do you think? The two samples really don’t look all that different.

Grow-able languages are truly exciting. If you want to add a feature to the language, you add it.

Programming with grow-able languages takes you from “power-user” to empowered-user!

Which would you rather be?


3 thoughts on “Rubys Upto in a Scheme macro”

  1. Maybe it’s just an OO bias, but I like the ruby one better still. I like types that have methods I guess.

    The scheme version is nice in that it allows you to have “flavor” words – from in this case, but it’s only nice if they are optional I think.

    It’s also poses an interesting question I guess. Writing a language is like writing a framework. Does it have any value in isolation of a program that solves a real problem? (Other than the value of academic interest.) Is that being overly pragmatic?

  2. RE: “an OO bias”…

    The fascinating thing is to look at syntaxes that really “speak to people”, and then to try and understand that appealing aspect of it. It just so happens that my laboratory has a lot of parentheses and a scientist who didn’t want to add “upto” to a type system in Scheme because it would’ve been more work :)

  3. RE: Writing a language is like writing a framework. Does it have any value in isolation of a program that solves a real problem?

    That is a good question to ask. I’ll read a little bit deeper in to it and rephrase the question as “Is it worth studying how to implement abstractions in a language with which you would never do any billable work?”.

    The answer is most assuredly yes, if you want to explore language as an abstraction. The important thing about undertaking such a study is that you have the tools to remove barriers to your learning; in other words, use the tools that make it easy. Ultimately, the important thing is that you internalize the techniques of abstraction, as you may then apply them using whatever programming language happens to be in vogue at the time.

    The programming language that you use to implement language as an abstraction is the least interesting thing about how you solve the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *