(sml)我可以得到一些帮助来实现统计案例数量的功能吗?

(sml)我可以得到一些帮助来实现统计案例数量的功能吗?,sml,Sml,我可以得到一些帮助来实现统计案例数量的函数吗 首先,我很抱歉一遍又一遍地问你同样的问题 我已经试着实现这个函数一个多星期了,但我还没有掌握它的窍门 这就是我现在写的代码 fun count (x,nil) = 0 | count(x,y::ys) = if x=y*2 then 2 else if x=y*4 then 4 else if x=y*10 then 10 else if x=y*20 then 20 else if

我可以得到一些帮助来实现统计案例数量的函数吗

首先,我很抱歉一遍又一遍地问你同样的问题

我已经试着实现这个函数一个多星期了,但我还没有掌握它的窍门

这就是我现在写的代码

    fun count (x,nil) = 0

    | count(x,y::ys) =

    if x=y*2 then 2

    else if x=y*4 then 4

    else if x=y*10 then 10

    else if x=y*20 then 20

    else if x=y*100 then 100

    else count(x,ys);
我知道这是一个非常无知的代码。但是我已经练习了实现你回答的函数,我根本不知道如何应用它们

这是我希望用c实现的代码

int count(int n, int arr[]) {
int cnt = 0;
int num1 = arr[0];

int num2 = arr[1];

if ((n % num1) == 0) cnt++;
if ((n % num2) == 0) cnt++;
if (((n - num1) % arr[1])==0) cnt++;

return cnt; 
}

int main() {
int n;
int arr[30];
int res = 0;

scanf("%d", &n);

scanf("%d %d", &arr[0], &arr[1]);

res = count(n, arr);

printf("%d", res);
}

如果我执行与c代码相同的操作,我希望实现number函数。我能得到一些帮助吗?

我不完全理解代码的意图,但是一个很好的技巧可能会有所帮助:

在C语言中,如果您发现自己正在更新一个局部变量,那么在SML中,您通常可以通过重新绑定该变量来完成相同的任务:

C:
  int x = 0;
  if (condition1) x++;
  if (condition2) x++;
  ...
  return x;

SML:
  let
    val x = 0
    val x = if condition1 then x+1 else x
    val x = if condition2 then x+1 else x
    ...
  in
    x
  end
使用这个技巧,翻译C函数应该很容易。剩下的我让你来做

只有一件事需要注意。在SML中以这种风格编写代码时,实际上根本没有更新局部变量。上面的SML代码与此等价,我们每次都创建一个新变量。用另一种风格写比较方便

 let
    val x0 = 0
    val x1 = if condition1 then x0+1 else x0
    val x2 = if condition2 then x1+1 else x1
    ...
  in
    x2
  end

非常感谢您的回复!你介意我再问你一个问题吗?我有数组0和索引1的值要比较。有没有办法接近sml中的每个元素,比如索引概念?这是不是太C语言问题了?实际上SML确实有数组。你可以这样做,例如
val a=Array.fromList[1,2,3]
,然后
val x=Array.sub(a,i)
使用一些索引
i
。这是界面:哦~~谢谢你的回复。我会再次努力学习。