I’ve got this OCaml codebase I’d like to convert to Scheme, partly to learn the language, partly because I keep getting hit by OCaml’s weird syntax.
Anyway, the aforementioned code makes heavy use of pattern matching. I’ve searched the documentation of DrScheme (now Racket), and found the module that seems to be perfect for me; however, I can’t seem to be able to make even the most basic example work: the code
(require scheme/match)
(define-struct tree (val left right))
(match (make-tree 0 (make-tree 1 #f #f) #f)
[(tree a (tree b _ _) _) (list a b)])
gives the error match: syntax error in pattern in: (tree a (tree b _ _) _)
Any of you have experience with pattern matching in Scheme?
>>4
most recent, #lang racket and #lang scheme both work
actually, to reproduce your error I had to comment out the struct definition
Name:
OP2010-07-16 13:27
>>5
I’ve just finished compiling the latest release of Racket, and indeed it works as expected.
I guess the problem, as usual, was sitting between the keyboard and the chair; sorry for wasting your time, but look at it another way: I’ve spent the last two days trying to get that code working, only to find the cause was an outdate Racket version.
There's some pattern matching libs for Scheme and CL, but I don't think they translate exactly the same as the O'Caml ones.
The closest to O'Caml's that I remember is Qi, which is a language in itself.
>>7
Indeed, the portable scheme pattern matchers tend to be written in plain R5RS so you don't get record matching. :(
Name:
OP2010-07-16 15:40
What I need is type matching, similar to that in the example code, and the scheme/match module in Racket seems to provide exactly what I need.
I would love to be portable, so any suggestion on how to do it is welcome; however, locking myself into an open–source implementation of Scheme doesn’t look too bad.
>>9 What I need is type matching
Well, you can do this portably, but it is often separate from the normal pattern matcher. There's no reason that this should be the case if you are using R6RS, but no-one has yet integrated the two. locking myself into an open–source implementation of Scheme doesn’t look too bad.
It isn't, except for when two incompatible Scheme have libraries that you would like to use in the same project :)
Name:
OP2010-07-17 13:36
>>10
Luckily, Racket seems to have a fairly comprehensive modules library.