In <f08q9v$f30$1@[EMAIL PROTECTED]
> Thomas Dybdahl Ahle
<lobais@[EMAIL PROTECTED]
> writes:
>Hi, I'm trying to read some (a lot of) sml code.
>I've read some tutorials and do***entation, so I believe I understand
>most of the sml syntax.
>However this line bugs me:
>case valOf (List.find match asciiweights) of (x, y, w) =>
> (w, Char.chr (pick (li, ri, x, y)))
>Besides I don't know what the valOf function does, I also have two other
>problems.
valOf is actually Option.valOf, and is defined as
fun valOf (SOME x) = x
so it unpacks the value of an option value.
>1) asciiweights is defined by
> val asciiweights = [
> ( 65, 91, 1),
> ( 97, 123, 1),
> ( 48, 58, 2),
> ( 32, 33, 3),
> ( 58, 65, 4),
> ( 91, 97, 4),
> (123, 127, 4),
> ( 33, 48, 4),
> (127, 256, 12),
> ( 1, 32, 25),
> ( 0, 1, 200)]
> and match is defined by "fun match (x, y, _) = ..."
>
> So how comes match takes three input variables but gets a list?
match does not get a list, List.find does. If you check, you will
see that List.find takes a function 'a -> bool and an 'a list and
returns an 'a option.
>2) When I read do***entation on case of => it normaly looks something
like
>fun fac n =
> case n of
> 0 => 1
> | _ => n * fac(n-1);
> Which is "case <number> of <condition> => return value"
>
> But in the line I'm investigating the condition has been switched by
>"(x, y, w)". How can that be?
case can be used as a switch, but can also be used do to pattern matching.
So what happens is that a value containing a triple is deconstructed to
its three comnponents. This compare it to
fun add (x, y, w) = x + y + z
add (1, 2, 3)
Here add is called with a value, which is a triple. The value is
deconstructed using pattern matching, so we are able to use the components
of the triple.
Instead of explicitly (or implicitly using fn) creating a function to
do the matching, case is used.
--
Michael Westergaard <mw@[EMAIL PROTECTED]
>
PhD student


|