作为函数使用列表cons操作符(a :: b) [英] Use list cons operator (a :: b) as a function

查看:172
本文介绍了作为函数使用列表cons操作符(a :: b)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

F#可以让你用 来把操作符变成函数:例如(+)类型为 int - > int - > int

F# lets you turn operators into functions by surrounding them with ( ): for instance, (+) is of type int -> int -> int.

是否可以使用list cons操作符 ::

Is it possible to do this with the list cons operator, ::?

它不像普通的二元运算符:

It doesn't behave like a normal binary operator:

FSI> (::);;

  (::);;
  -^^

c:\temp\stdin(3,2): error FS0010: Unexpected symbol '::' in expression.
Expected ')' or other token.

List.Cons 方法需要一个元组;它不会受到干扰。

And the List.Cons method takes a tuple; it's not curried.

(能够做到这一点很有用,例如,您可以使用它来实现折叠图)。

(It's useful to be able to do this. For instance, you can use it to implement map in terms of fold).

推荐答案

来自 http://cs.hubfs.net/forums/permalink /11713/11713/ShowThread.aspx#11713

(::)是一个区别联合列表的'构造函数'<'>键入,因此引发了一个问题:是否应将其参数作为函数值进行curried(如 + )或元组化(与所有DU构造函数一样) 。无论哪种方式,对某些人来说似乎都是鱼腥味/令人意想不到的,所以F#只是不允许构造。

(::) is a discriminated union 'constructor' for the list<'a> type, and so raised the question of whether as a function value its arguments should be curried (like +) or tupled (like all DU constructors). Either way seems fishy/unexpected to some people, so F# simply disallows the construct.

当然,你总是可以编写例如

Of course you can always write e.g.

let cons x y = x :: y

并使用 cons ,或者使用lambda fun xy - > x :: y ,如果你想要一个两个参数的curried前缀函数。

and use cons, or just use a lambda fun x y -> x::y, if you want a "curried prefix function of two args" for this.

这篇关于作为函数使用列表cons操作符(a :: b)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆