| Next | Functional Programming in Perl | 21 |
Here's a most substantial example
In ML for the Working Programmer, Paulson presents a parsing system
A parser is a function of type:
char list -> 'a * char list
For example:
fun digit (in1::ins) =
if in1 = '0' then (0, ins) else
if in1 = '1' then (1, ins) else
...
if in1 = '9' then (9, ins) else
raise "Parse error..."
Now you want some parser combinators:
fun nothing input = ((), input);
fun concat p1 p2 input =
let val (v1, i1) = p1 input in
let val (v2, i2) = p2 i1 in
((v1, v2), i2)
end end
fun star p1 input =
alternates(concat(p1, star p1), nothing);
| Next | ![]() |
Copyright 2005 M. J. Dominus |