F#不包含一些在哈斯克尔(主要是因为F#程序员通常更喜欢编程的明确的风格,只在最明显的情况下,使用pointfree风格中提供的基本功能,它不会影响可读性)。
但是你可以定义这样几个基本的组合子:
// 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)
现在,您可以实现添加使用组合算符两个字符串的长度的功能如下:
let addLengths =
uncurry (((first String.length) >> (second String.length)) >> (curry (+)))
这种构造两个功能将String.length
应用于元组的第一个/第二个元素,然后进行组合,然后使用+
添加元组的元素。整个东西被包装在uncurry
中,所以你得到string -> string -> int
类型的功能。
你应该使用“无点”而不是“无意义”。这是标准术语。 :) – 2010-06-04 03:19:04