Thoughts on Perl and Emacs, technology and writing

Pattern matching in scheme

I’ve been looking for an excuse for familiarising myself with PLT scheme’s pattern matching library for a while. Finally, I came across an article about implementing a basic Computer Algebra System. The Ocaml source code can be found here. Using the pattern matching library it can be fairly mechanically translated into scheme.

If I had come across this post a few weeks ago, I probably wouldn’t have even considered the implementation using define-struct. HTDP has certainly improved the way I think about coding scheme now and indeed several of the exercises in the book involve constructing various scheme evaluators that embed a simple CAS like the one above.

The article concludes with the following quote:

“…as you can see, Ocaml’s variant types and pattern matching are a perfect fit for the problems a programmer writing a CAS would face. In fact, few other languages, with the possible exception of Haskell, would have fit this problem as well.”

It seems that with the appropriate extensions, scheme can be a good fit for this type of problem too.

Advertisements

Share this:

Like this:

Related

One Response

I have been using my own customized version of scheme (it’s a preprocessor). One of my favorite things is a pattern matching syntax that I modeled superficially on Haskell. I tried to make it as concise as possible. It serves both as a destructive
pattern-matching system, a constructor-based algebraic type system, and an overloading system.

It’s only superficially like Haskell — it’s of course latently typed, like regular Scheme. However, I have found it to be incredibly useful. I also used to have the problem that I wrote Scheme with a ‘C++ accent’, and this pattern-matching stuff totally cured me of it.

Below is an example of some code implementing linear transformations for various types.