VAL keyword within IF condition won't allow reassignment in SML

放肆的年华 提交于 2019-12-11 14:45:47

问题


Given this following code (which does not work):

fun func() = 

val decimal = 0 (* the final result *)
val multiple = 0 (* keeps track of multiples, eg. In XXV, X would be a multiple *)
val current = 0 (* the digit currently being processed *)
val top = 0   (* value of the last element in the list *)
val last_add = 0 (* the last digit that wasn't a multiple, or subtraction operation *)
val last_sub = 0
val problem = 0 (* if value is 1 then there is a problem with the input *)

val myList = [1,2,3,4,5] (* the list has more values *)
val current = tl(myList) (* grab the last element from the list *)
val myList = tl(myList) (* remove the last element from the list *)
val top = tl(myList) (* grab the value at the end of the list *)



while (not(myList = []))    (* run while the list is not empty *)

if ( (not(myList = [])) andalso (current > top))
   then      

            val decimal = decimal + current - top
            val last_sub = top;
            val myList = tl(myList)
   else     
       if ( (myList = []) andalso (current = top))
          then val decimal = decimal + current
               val multiple = multiple + 1
          else
              if (last_sub = current)
                 then val problem = 1

                 else
                      val decimal = decimal + current
                      val multiple = 0
                      val last_add = current

This is only a partial code , which doesn't work at the moment , since the val is not possible within an if statement .

  1. I want to run in a while loop , how can I do that in ML ?

  2. How can I assign and reassign values into variables that were previously declared in ML ?

  3. The val keyword is not possible within the IF condition , so I cannot update the variables , any idea how to solve that ?

Regards


回答1:


How can I assign and reassign values into variables that were previously declared in ML ?

You cannot assign to variables after they are declared in ML.

The val keyword is not possible within the IF condition , so I cannot update the variables , any idea how to solve that ?

Except at the top level, you usually use val and fun inside a let:

let
  val x = blah blah
  val y = blah blah
  fun f x y = blah blah
in
  some expression
end

However, note that this creates a new variable (which may hide any existing variable of the same name), which exists inside the scope of the body of the let. As said before, you cannot assign to an existing variable.

I want to run in a while loop , how can I do that in ML ?

You are almost there. The syntax is while condition do ( ... ). But a while loop is useless without mutable state.

If you want mutable state, you can use a mutable data structure. The language provides a simple "mutable cell" called ref: you create it by passing the initial value to the ref function, you get the current value with the ! operator, and you set a new value with the := operator. You also have to remember that if you want to run multiple imperative "statements", you must separate them with the ; operator, and possibly enclose the whole "block" of statements in parentheses due to precedence issues.

But using while loops and mutable state is really not the right way to go here. You are using a functional language, and it would be much better for you to re-write your algorithm to be purely functional. It's not hard to do. You would turn the body of your while loop into a tail-recursive helper function, and the "variables" that change between iterations of the loop would become arguments to this function. Instead of trying to "set" the values of these variables, it would simply recursively call itself with the new values for the next iteration. If it's tail-recursive, it's equivalent to iteration memory-wise.



来源:https://stackoverflow.com/questions/14071226/val-keyword-within-if-condition-wont-allow-reassignment-in-sml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!