机器学习新手:如何存储 a* a* a* 类型的返回值

发布于 2024-09-19 18:44:28 字数 267 浏览 10 评论 0原文

我有一个返回 int*int 的程序

(用于说明目的的示例): fun program(a,b) = (1,2)

我想做一些类似的事情:

趣味节目(a,b)
如果 a = 0 则 (1,2)
否则
val x,y = 程序(a-1,b)
返回(x-1,y)

基本上,我想操作返回的元组,然后返回它的修改。

谢谢

I have a program that returns int*int

(Example for illustration purposes):
fun program(a,b) = (1,2)

I want to do something along the lines:

fun program(a,b)
if a = 0 then (1,2)
else
val x,y = program(a-1,b)
return (x-1, y)

Basically, I want to manipulate the tuple that is returned, and then return a modification of it.

Thanks

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

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

发布评论

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

评论(2

静待花开 2024-09-26 18:44:38

我还想建议,虽然您需要返回类型t : int * int,但您至少curry< /strong> 函数的输入来自: int * int = int -> int。如果您需要将其转回原处,您可以随时取消咖喱。我建议将该函数编写为:

fun program a b =
   if a = 0 
   then (1,2)
   else
       let
           val (x,y) = program (a - 1) b
       in
           (x - 1, y)
       end

该函数的类型是: int ->整数-> (整数*整数)

I'd also like to suggest that while you need to return a type t : int * int, you can atleast curry the inputs to your function from : int * int = int -> int. If you need to turn this back, you can always uncurry. I'd suggest writing the function as :

fun program a b =
   if a = 0 
   then (1,2)
   else
       let
           val (x,y) = program (a - 1) b
       in
           (x - 1, y)
       end

The type of this function is : int -> int -> (int * int)

煮酒 2024-09-26 18:44:36

这几乎与您编写的完全一样,只是您的语法有点偏离:

fun program(a,b) =
  if a = 0 then (1,2)
  else
    let val (x,y) = program(a-1,b) in
      (x-1, y)
    end

具体来说:

  1. 函数由 fun f args = body 定义 - 您遗漏了 =
  2. 变量通过 let val foo = bar in baz end 绑定。
  3. sml 中没有 return 关键字。

This works almost exactly as you wrote it, except that your syntax is a bit off:

fun program(a,b) =
  if a = 0 then (1,2)
  else
    let val (x,y) = program(a-1,b) in
      (x-1, y)
    end

Specifically:

  1. Functions are defined by fun f args = body - you left out the =.
  2. Variables are bound with let val foo = bar in baz end.
  3. There is no return keyword in sml.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文