标准的ml循环麻烦 [英] Standard ml loop troubles
问题描述
我正在设置一个函数,该函数将模拟循环直到满足条件为止.
I am setting up a function that will simulate a loop until a condition is met.
我的总体计划是使用递归,但是我试图先降低基础知识.
My overall plan is to use recursion but I am trying to get the basics down first.
我有一个使用If语句的基本函数,该语句查看X的值是什么.我打算使用递归将X用作计数器,但稍后我会介绍.
I got a basic function working using an If statement that is seeing what the value of X is. I plan to use recursion to use X as an counter but I will get to that later.
我现在主要关心的是,似乎我只能在"then"语句之后执行1条命令.
My main concern right now is, it seems I can only do 1 command after the "then" statement.
fun whileloop (x,a) =
if (x<4)
then a+1
else a;
因此,此功能可以正常工作,但似乎我唯一可以执行的命令是a + 1.如果之后我尝试执行其他任何命令,则在else ...之前失败.
So this function works perfectly fine, but it seems the only command I can do is the a+1. If I try to perform any other command after that, before the else...it fails.
例如,以下代码将对我失败.
For example, the below code will fail on me.
fun whileloop (x,a) =
if (x<4)
then a+1
print "Testing"
else a;
我的最终目标是创建一个循环,该循环将反复执行多个动作,直到X达到零为止.我需要使用不同的功能执行类似5-6的操作.
my ultimate goal is to create a loop that will perform several actions over and over until X reaches zero. I need to perform like 5-6 actions using different functions.
推荐答案
您可以使用分号运算符按顺序求值多个表达式:
You can evaluate several expressions in sequence using the semicolon operator:
( e1; e2; ...; eN )
例如,
fun iter n f = if n = 0 then () else (f n; iter (n-1) f)
这篇关于标准的ml循环麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!