2
下面的函數createTuple
可以表示爲pointfree嗎?創建一個元組pointofree
let createTuple = fun v -> (v, v*2)
createTuple 2 |> printfn "%A" // (2,4)
下面的函數createTuple
可以表示爲pointfree嗎?創建一個元組pointofree
let createTuple = fun v -> (v, v*2)
createTuple 2 |> printfn "%A" // (2,4)
的F#庫沒有爲自由點式編寫代碼(主要是因爲它是不寫F#的特別習慣的方法)提供了許多功能,所以你不能只使用什麼是可用在寫你的createTuple
功能核心圖書館。
如果你真的要做到這一點,你可以定義幾個幫手組合子與元組的工作:
/// Duplicates any given value & returns a tuple with two copies of it
let dup a = a, a
/// Transforms the first element using given function
let mapFst f (a, b) = (f a, b)
/// Transforms the second element (not needed here, but adding for symmetry)
let mapSnd f (a, b) = (a, f b)
有了這些,你可以在自由點的方式實現的功能:
let createTuple = dup >> mapSnd ((*) 2)
這和你的功能是一樣的。我認爲解讀這裏發生的事情是非常困難的,我絕對不會寫這些代碼,但這是另一個問題:-)。
正是我想要的,謝謝! – rickythefox
你是指'(,)'運算符,就像在Haskel/Elm中那樣? –