我試圖爲給定的布爾表達式生成一個真值表。我可以通過創建一個新的數據類型BoolExpr來做到這一點,但我想用一個匿名函數來做到這一點。它應該是這樣工作的:Haskell中匿名函數的真值表
> tTable (\x y -> not (x || y))
output:
F F | T
F T | F
T F | F
T T | F
我的方法:
tbl p = [(uncurry p) tuple | tuple <- allval]
where allval=[(x,y) | x <- [False,True], y <- [False,True]]
這工作,但只有2個參數。我想爲任何數量的參數做它。所以,我想我會做一個函數,從列表中取參數:
argsFromList f [] = f
argsFromList f (x:xs) = argsFromList (f x) xs
這不起作用:
Occurs check: cannot construct the infinite type: t = t1 -> t
Expected type: t -> [t1] -> t1 -> t
Inferred type: (t1 -> t) -> [t1] -> t1 -> t
In the expression: argsFromList (f x) xs
我不明白的問題是在這裏。 如果有人能指出我正確的方向或發佈鏈接,我將不勝感激。
可能的重複[爲什麼在Haskell中不允許這樣的函數定義?](http://stackoverflow.com/questions/6168880/why-is-such-a-function-definition-not-allowed-in- haskell) –
請注意,您可以將lambda定義爲'(\ [x,y] - > not(x || y))',這會自動爲您提供「具有任意多個參數的函數的所有相同類型的行爲」。 – leftaroundabout