这可以用点自由风格表达吗? [英] Can this be expressed in point free style?

查看:21
本文介绍了这可以用点自由风格表达吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下表达式来对 IEnumerable 的数字求和:

Given the following expression to sum an IEnumerable of numbers:

let sum l = l |> Seq.reduce(+)  //version a

是否有可能消除争论——就像这样?

is it possible to eliminate the argument--like so?

let sum = Seq.reduce(+)    //version b

我从 F# 编译器 (FS0030) 收到一个错误,我似乎记得我看到过一些关于eta 转换"的内容,但不幸的是,我对 lambda calc 的了解太有限,无法了解 eta 转换是如何涉及的.

I get an error from the F# compiler (FS0030) and I seem to recall having seen something about an "eta conversion" being involved but unfortunately my knowledge of lambda calc is too limited to follow how eta conversion is involved.

是否可以像版本 b 一样消除论证?

Can the argument be eliminated as in version b?

有人请我指点一下可以解释 eta 转换以及它如何在这段特定代码中发挥作用的文献吗?

Would someone please point me to literature that would explain an eta conversion and how it would come into play in this particular piece of code?

FS0030:

stdin(1,5):错误 FS0030:值限制.'sum' 的值是推断具有泛型类型val sum : ('_a -> int) when '_a :> seq 要么使'sum'的参数显式,或者,如果你不打算让它成为泛型,添加类型注解.

stdin(1,5): error FS0030: Value restriction. The value 'sum' has been inferred to have generic type val sum : ('_a -> int) when '_a :> seq Either make the arguments to 'sum' explicit or, if you do not intend for it to be generic, add a type annotation.

推荐答案

Eta 转换"仅表示添加或删除参数.您遇到的问题称为值限制.在 ML 语言中,一个值声明为一个值,即.没有显式参数声明,不能有泛型类型,即使它有一个函数类型.这里是一些相关文献.这个想法是为了防止引用单元格保存不同类型的值.例如,如果没有值限制,将允许以下程序:

"Eta conversion" simply means adding or removing the argument. The problem you are hitting is called value restriction. In ML languages, a value declared as a value, ie. declared without explicit arguments, cannot have a generic type, even if it has a function type. Here is some relevant literature. The idea is to prevent a ref cell from holding values of different types. For example, without value restriction, the following program would be allowed:

let f : 'a -> 'a option =
    let r = ref None
    fun x ->
        let old = !r
        r := Some x
        old

f 3           // r := Some 3; returns None : int option
f "t"         // r := Some "t"; returns Some 3 : string option!!!

正如kvb所说,如果你不打算让函数通用,那么你可以添加类型签名并使用point-free风格.

As kvb said, if you do not intend the function to be generic, then you can add a type signature and use point-free style.

这篇关于这可以用点自由风格表达吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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