6

It's not a practically important issue, but I'd like to see an example of tacit programming in F# where my point-free functions can have multiple arguments (not in form of a list or tuple).

And secondly, how such functions can manipulate a complex data structure. I'm trying it out in F# Interactive, but have no success yet.

I tried, for instance:

> (fun _ -> (fun _ -> (+))) 333 222 111 555

Is that right way?

And:

> (fun _ -> (fun _ -> (+))) "a" "b" "c" "d";;  

val it : string = "cd"
4

2 に答える 2

4

F#には、Haskellで使用できる基本的な関数の一部が含まれていません(主に、F#プログラマーは通常、明示的なプログラミングスタイルを好み、読みやすさを損なわない最も明白な場合にのみポイントフリースタイルを使用するためです)。

ただし、次のようないくつかの基本的なコンビネータを定義できます。

// turns curried function into non-curried function and back
let curry f (a, b) = f a b
let uncurry f a b = f (a, b)

// applies the function to the first/second element of a tuple
let first f (a, b) = (f a, b)
let second f (a, b) = (a, f b)

これで、次のようにコンビネータを使用して2つの文字列の長さを追加する関数を実装できます。

let addLengths = 
  uncurry (( (first String.length) >> (second String.length) ) >> (curry (+)))

これは、タプルの1番目と2番目の要素に適用される2つの関数を作成しString.length、それらを構成してから、を使用してタプルの要素を追加し+ます。全体がに包まれてuncurryいるので、タイプの関数を取得しますstring -> string -> int

于 2010-05-08T12:45:35.070 に答える
2

F# では、関数のアリティが固定されているため、両方を記述することはできません。

(op) 1 2

(op) 1 2 3 4

特定の operator に対してop。それが必要な場合は、リストまたはその他のデータ構造を使用する必要があります。名前付き変数を避けたいだけなら、いつでも「1 + 2 + 3 + 4」を実行できます。F# で数値のリストを追加する最も慣用的な方法はList.sum [1;2;3;4]、変数も回避する です。

于 2010-05-08T15:45:36.157 に答える