Can this be expressed in point free style?

后端 未结 3 1052
心在旅途
心在旅途 2020-12-19 16:46

Given the following expression to sum an IEnumerable of numbers:

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

is it possible to eliminate

3条回答
  •  有刺的猬
    2020-12-19 17:21

    "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!!!
    

    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.

提交回复
热议问题