2870Re: 'ocaml_beginners':: Re: 'ocaml_beginners':: Is it possible to get an instantiatable pattern matching?
- Jan 1, 2005pattern matching can be implemented using recursive descent parsing, which
is described in cousineau and mauny (using ocaml!).
the idea is that you want to construct a function that takes some
structure and either performs an action (binding values to names in a
hashtable) or "fails". if you use explicit state (pure functional
programming) you can backtrace on failure and try alternative patterns
quite easily. and it turns out that you can do all this using higher
the data you are matching is read from a stream. streams are a lot like
lists, with a head and tail operation. so you take the head of the
stream, see if you can match it and, if so, pass the tail on to the next
bit of matching. if not, you return None and something else gets a chance
to match the stream.
i was going to post an explanation in more detail, but i'm realising that
it's quite some work! search around for "recursive descent" "parsing"
"streams" and try cousineau + mauny. there's also some info at around
page 110 of the ocaml book (developing applications...)
warning - i think both c&m and the ocaml book use ocaml's own streams.
i've not used those and they may behave slightly different to the model i
have in my head, but i've hardly given any details here so i doubt there's
an important contradiction.
i'd really encourage you to learn about this stuff - i found it took a
while to understand, but once i actually implemented the code myself, it
made sense and has been one of the most useful things i've ever learnt in
` __ _ __ ___ ___| |_____ work web site: http://www.ctio.noao.edu/~andrew
/ _` / _/ _ \/ _ \ / / -_) personal web site: http://www.acooke.org/andrew
\__,_\__\___/\___/_\_\___| list: http://www.acooke.org/andrew/compute.html
- << Previous post in topic