SML NJ 的内部函数

发布于 2025-01-07 11:48:27 字数 399 浏览 1 评论 0原文

我是 sml 的新手,并且在内部函数的语法方面遇到了麻烦。我需要做的是获取一个整数列表的列表,对每个列表求平均值,然后返回一个实数列表。这是我到目前为止的伪代码。

 fun listAvg [] = 0  
    else (sum (x) div size (x))

        fun sum[] = 0
        | sum(head::rest)= head + sum rest;


        fun size [] = 0
        | size(head::rest) = 1 + size rest;

    listAvg([[1,3,6,8,9], [4,2,6,5,1], [9,5,9,7], [5,4], [3,6,4,8]]); 

任何建议将不胜感激。谢谢!

I am a complete newbie to sml and am having trouble with the syntax for inner functions. What I need to do is take a list of a list of ints, average each list, and return a list of reals. This is the psuedo-ish code I have so far.

 fun listAvg [] = 0  
    else (sum (x) div size (x))

        fun sum[] = 0
        | sum(head::rest)= head + sum rest;


        fun size [] = 0
        | size(head::rest) = 1 + size rest;

    listAvg([[1,3,6,8,9], [4,2,6,5,1], [9,5,9,7], [5,4], [3,6,4,8]]); 

any advice would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2025-01-14 11:48:27

使用 let,如

fun listAvg [] = 0  
  | listAvg x =
    let
        fun sum[] = 0
          | sum(head::tail)= head + sum tail;
        fun size [] = 0
          | size(head::tail) = 1 + size tail;
     in
         (sum x) div (size x)
     end

您必须将 int list 传递给此函数,例如,

listAvg [1, 2, 3, 4];

除了重新排列顺序并放置关键字 let 之外,这不会对您的代码进行任何更改, 结束。如果这不是家庭作业,我建议在 List 结构中使用一些内置标准库函数,可以将此函数减少到两行,包括空列表上的模式匹配。

编辑

“对整数列表进行平均”有两种可能的含义。第一种方法是对每个列表求平均值,然后取平均值,第二种方法是将这些列表连接成一个长列表,然后对整个列表求平均值。当所有整数列表的长度相同时,这两种方法是等效的(舍入误差除外),但如您的示例所示,它们的长度不必相同。

由于这是作业,我不会直接给您答案,但请考虑以下内容,这可能会有所帮助:

  • 如果您使用“平均整数列表的列表:”的第一种解释,则有一个内置 SML 函数可让您将另一个函数应用于列表的每个元素,并获取结果列表。这可能有助于获得个人平均值,然后您可以将其合并为总体平均值。
  • 如果您使用第二种解释:有一个内置的 SML 函数(它看起来像一个运算符,但 SML 中许多看起来像运算符的东西只是中缀函数)将两个列表连接在一起,并且有一个内置的在 SML 函数中将函数应用于列表中的元素以及累加器值,以生成单个值。您也许可以使用这两个函数创建所有数字的一长列表,然后对其进行平均。

Use a let, as in

fun listAvg [] = 0  
  | listAvg x =
    let
        fun sum[] = 0
          | sum(head::tail)= head + sum tail;
        fun size [] = 0
          | size(head::tail) = 1 + size tail;
     in
         (sum x) div (size x)
     end

You have to pass an int list to this function e.g.

listAvg [1, 2, 3, 4];

This is no change to your code, except to rearrange the order and putting the keywords let, in, and end. If this is not homework, I would recommend using a few built-in standard library functions in the List structure that could reduce this function to two lines, including the pattern match on the empty list.

EDIT:

There are two possible meanings to "average a list of a list of ints." The first is to average each list and then take the average of the averages, and the second is to join the lists together into one long list and take the average over the entire list. The two methods are equivalent (except for rounding errors) when all the lists of ints are of the same length, but as your example shows, they don't have to be the same length.

Since this is homework, I'm not going to give you the answer directly, but consider the following, which might be helpful:

  • If you're using the first interpretation of "average a list of a list of ints:" there is a built-in SML function that will let you apply another function to each element of a list, and get the resulting list. This might be helpful for getting the individual averages, which you can then combine together into an overall average.
  • If you're using the second interpretation: there is a built-in SML function (it looks like an operator, but many of the things that look like operators in SML are just infix functions) to join two lists together, and a built-in SML function to apply a function to elements going down the list, along with an accumulator value, to generate a single value. You might be able to use those two functions to create one long list of all the numbers, which you can then average.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文