标准机器学习:如何在函数内执行另一个函数?

发布于 2024-08-16 18:11:21 字数 354 浏览 3 评论 0原文

目前,我的代码如下所示:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0);
        val void = mapi gradient iImg;
    in
        Gdimage.toPng iImg "gradient.png"
    end;

mapi 是一个类型为 intint->intint*int->image->unit 的函数。本质上它对提供的图像进行操作。

该函数看起来很难看 val void = ...

我怎样才能消除它?

Currently, my code looks like this:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0);
        val void = mapi gradient iImg;
    in
        Gdimage.toPng iImg "gradient.png"
    end;

mapi is a function with type intint->intint*int->image->unit. Essentially it operates on the image supplied.

The function looks ugly with val void = ...

How could I eliminate that?

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

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

发布评论

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

评论(3

盗梦空间 2024-08-23 18:11:21

您可以在 let 表达式中的 inend 之间拥有一个表达式列表。因此,您可以将代码重写为

fun gradImage () =
    let
        val iImg = Gdimage.image(640,480) (0,0,0)
    in
        mapi gradient iImg;
        Gdimage.toPng iImg "gradient.png"
    end;

我假设 mapi 修改了 iImg ,因为代码似乎是这样编写的。这听起来不太实用;让 mapi 返回修改后的 image 会感觉更自然,但从我对 Gdimage 接口的了解来看,它看起来像是在那里完成的,我理解它是从效率的角度来看可能更好。

You can have a list of expressions between in and end in a let expression. So you could rewrite your code to

fun gradImage () =
    let
        val iImg = Gdimage.image(640,480) (0,0,0)
    in
        mapi gradient iImg;
        Gdimage.toPng iImg "gradient.png"
    end;

I assume mapi modifies iImg in place, as the code seems to be written that way. That doesn't sound very functional; it would feel more natural to have mapi return the modified image, but from what I can see of the Gdimage interface, it looks like how it's done there, and I understand it's probably better from efficiency perspective.

衣神在巴黎 2024-08-23 18:11:21

我使用 SML 已有十年了,但我相信您正在寻找的是:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0)
    in
        ignore (mapi gradient iImg);
        Gdimage.toPng iImg "gradient.png"
    end;

It's been a decade since I've used SML, but I believe what you're looking for is this:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0)
    in
        ignore (mapi gradient iImg);
        Gdimage.toPng iImg "gradient.png"
    end;
人│生佛魔见 2024-08-23 18:11:21

实际上,我更喜欢提供的额外类型检查。

val () = mapi gradient iImg

如果这看起来很难看,它应该是这样的——它不是很实用,而标准 ML 绝对是一种函数式语言。

I actually prefer the additional type-checking offered by

val () = mapi gradient iImg

If this looks ugly, it should—it's not very functional, and Standard ML is definitely a functional language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文