Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ml > Re: Problems un...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 9 Topic 869 of 973
Post > Topic >>

Re: Problems understanding sml code

by Michael Westergaard <mw@[EMAIL PROTECTED] > Apr 20, 2007 at 06:10 PM

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
 




 9 Posts in Topic:
Problems understanding sml code
Thomas Dybdahl Ahle <l  2007-04-19 22:26:07 
Re: Problems understanding sml code
torbenm@[EMAIL PROTECTED]  2007-04-20 18:09:39 
Re: Problems understanding sml code
jmv16@[EMAIL PROTECTED]   2007-04-20 18:10:37 
Re: Problems understanding sml code
Michael Westergaard <m  2007-04-20 18:10:48 
Re: Problems understanding sml code
Thomas Dybdahl Ahle <l  2007-04-26 18:45:58 
Re: Problems understanding sml code
lobais <lobais@[EMAIL   2007-04-26 18:48:11 
Re: Problems understanding sml code
"Albert Y. C. Lai&qu  2007-05-09 00:05:49 
Re: Problems understanding sml code
jmv16@[EMAIL PROTECTED]   2007-05-09 00:29:22 
Re: Problems understanding sml code
Andreas Rossberg <ross  2007-05-09 00:29:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 19:54:47 CDT 2008.