我想了解如何將函數轉換爲Haskell中的無點表示法。我看到this example,但它比我所尋找的更復雜。我覺得我理解它背後的邏輯,但是當我嘗試在代碼中執行一些簡單的示例時,我收到了編譯錯誤。我想嘗試寫在卡點式這樣的功能:簡單的哈斯克爾函數的免點風格
f x = 5 + 8/x
我重新排列f x = (+) 5 $ (/) 8 x
所以,我想這可能是這樣的:
f = (+) 5 $ (/) 8
但是當我運行這在ghci我得到這個消息:
No instance for (Num (a0 -> a0))
arising from the literal `5' at Test.hs:3:9
Possible fix: add an instance declaration for (Num (a0 -> a0))
In the first argument of `(+)', namely `5'
In the first argument of `($)', namely `(+) 5'
In the expression: (+) 5 $ (/) 8
Failed, modules loaded: none.
我不明白「沒有實例......」的消息。我需要做什麼來以無點式的方式編寫這個函數?
我想你可能會對[$'和'.'運算符之間的差異感到困惑」(http://stackoverflow.com/questions/940382/haskell-difference-between-dot-and-dollar-sign )。 – hammar