問題描述
如何為 lambda 中的運算符賦予不定性? (How to give infixities to operators in lambda?)
例如,這不會輸入檢查
\cons nil ‑> 5 `cons` 3 `cons` nil
也不會
\(#) ‑> 5 # 3 # nil
雖然這兩個都可以
\cons nil ‑> 5 `cons` nil
\(#) nil ‑> 5 # nil
有沒有辦法分配不定點給 lambdas 中的運算符。我試過了
infixr 5 #
foo = \(#) nil ‑> 5 # 3 # nil
它給出了一個沒有定義#
和
foo = \(infixr 5 #) nil ‑> 5 # 3 # nil
這只是一個語法錯誤的錯誤。
什麼可以嗎?
參考解法
方法 1:
Fixity declarations can be local but must accompany definitions, so you'd have to write something like
foo cons nil = 'a' # 'b' # nil
where (#) = cons
infixr 5 #
or
foo = \cons nil ‑> let (#) = cons; infixr 5 # in 'a' # 'b' # nil
etc.
(by PyRulez、Reid Barton)