IF条件内的VAL关键字不允许在SML中重新分配 [英] VAL keyword within IF condition won't allow reassignment in SML

查看:150
本文介绍了IF条件内的VAL关键字不允许在SML中重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码(不起作用):

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

这只是一个部分代码,目前不起作用,因为val不是 可能在if语句中.

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

  1. 我想在while循环中运行,如何在ML中做到这一点?

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

如何将值分配和重新分配给先前在ML中声明的变量?

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

在IF条件下无法使用val关键字,因此我无法更新变量,如何解决该问题?

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

致谢

推荐答案

如何将值分配和重新分配给以前的变量 以前在ML中声明过?

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

在ML中声明变量后,您将无法为其赋值.

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

在IF条件下val关键字是不可能的,所以我不能 更新变量,知道如何解决吗?

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

除了在顶层,通常在let中使用valfun:

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

但是,请注意,这会创建一个 new 变量(该变量可能隐藏任何同名的现有变量),该变量存在于let主体的范围之内.如前所述,您无法分配给现有变量.

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.

我想在while循环中运行,如何在ML中做到这一点?

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

您快到了.语法为while condition do ( ... ).但是,如果没有可变状态,则while循环是无用的.

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

如果想要可变状态,则可以使用可变数据结构.该语言提供了一个称为ref的简单可变单元格":您可以通过将初始值传递给ref函数来创建它,使用!运算符获取当前值,然后使用:=运算符.您还必须记住,如果要运行多个命令式语句",则必须使用;运算符将它们分开,并且可能由于优先级问题而将整个语句的块"括在括号中.

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.

但是使用while循环和可变状态确实不是正确的方法.您正在使用功能性语言,因此将算法重新编写为纯功能性会更好.这并不难.您可以将while循环的主体转换为尾递归辅助函数,并且在循环的迭代之间更改的变量"将成为该函数的参数.与其尝试设置"这些变量的值,不如简单地以新值递归地调用自身,以进行下一次迭代.如果是尾递归,则相当于在内存上迭代.

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.

这篇关于IF条件内的VAL关键字不允许在SML中重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆