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: Destructive...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 5 Topic 868 of 973
Post > Topic >>

Re: Destructive Assignment in SML

by torbenm@[EMAIL PROTECTED] (Torben Ęgidius Mogensen) Apr 19, 2007 at 10:24 PM

Rob Hoelz <hoelz@[EMAIL PROTECTED]
> writes:

> I won't lie; this is for a homework assignment, but I'm not asking it
> to be done for me; I just want to know how to accomplish destructive
> assingment in ML.  The problem is to construct a function with
> memoization, so here's what I have:
>
> local
>     val memory = []
> in
>   fun fastF 0  = 1 | 
>     fastF ~1 = 0 | 
>     fastF ~2 = 0 | 
>     fastF ~3 = 0 | 
>     fastF m  =   
>         let 
>             val result = find (fn (key, value) => key = m) memory
>         in  
>             if isSome result then
>                 #2(result)
>             else
>               let 
>                 val result = fastF(m-1) + fastF(m-2) + fastF(m-3) -
>                     fastF(m-4) 
> 	      in  
>                 ((val memory = ((m, result) :: memory)); result)
>               end 
>         end 
>
> Unfortunately, (val memory = ...) doesn't accomplish what I need; it
> causes a syntax error.  Could somone shed some light on how I could
> assign to memory?  Thanks!

Variables in SML can't be destructively assigned to, so the third-last
line just creates a new local variable called memory.

But SML has references that can be overwritten.  Note that when you
have a variable that holds a reference value, it is not the variable
that is overwritten, but the reference value that it holds.

So, start your program with "val memory = ref []".  Use "!memory" when
looking up the value of the reference and "memory := ..." to assign to
it.

	Torben
 




 5 Posts in Topic:
Destructive Assignment in SML
Rob Hoelz <hoelz@[EMAI  2007-04-17 21:42:58 
Re: Destructive Assignment in SML
torbenm@[EMAIL PROTECTED]  2007-04-19 22:24:34 
Re: Destructive Assignment in SML
"Albert Y. C. Lai&qu  2007-04-19 22:25:05 
Re: Destructive Assignment in SML
dbenson@[EMAIL PROTECTED]  2007-04-19 22:25:15 
Re: Destructive Assignment in SML
Vesa Karvonen <vesa.ka  2007-04-19 22:25:40 

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 14:58:14 CDT 2008.