Function Haskell xor不适用于映射
我在Function Haskell xor不适用于映射,function,haskell,xor,Function,Haskell,Xor,我在Data.Bits模块中使用xor函数时遇到问题 就像下面的代码一样 当我尝试应用参数为[1..10]和[2..11]的和func时(参数只是任意数组) 它起作用了。(不在此处写入,但orFunc(.|)也可以使用) 但由于某些原因,xorFunc没有。。。。说 <interactive>:74:1: error: ? Non type-variable argument in the constraint: Enum ((a -> a ->
Data.Bits
模块中使用xor
函数时遇到问题
就像下面的代码一样
当我尝试应用参数为[1..10]
和[2..11]
的和func
时(参数只是任意数组)
它起作用了。(不在此处写入,但orFunc(.|)
也可以使用)
但由于某些原因,xorFunc
没有。。。。说
<interactive>:74:1: error:
? Non type-variable argument
in the constraint: Enum ((a -> a -> a) -> t -> c)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall a t c.
(Enum ((a -> a -> a) -> t -> c), Enum t,
Num ((a -> a -> a) -> t -> c), Num t, Bits a) =>
[c]
:74:1:错误:
? 非类型变量参数
在约束中:Enum((a->a->a)->t->c)
(使用flexibleContext允许此操作)
? 检查推断类型时
它:对于所有的t c。
(枚举((a->a->a)->t->c),枚举t,
Num((a->a->a)->t->c),Num t,位a)=>
[丙]
你知道为什么吗
运行环境:
GHC 8.2.1不带标志
Windows 10 64位如果要使用中缀表示法中的函数,必须使用反勾号语法
xorFunc :: [Int] -> [Int] -> [Int]
xorFunc xs ys = zipWith (\x y -> x `xor` y) xs ys
但是这个问题可以通过不把它写成lambda表达式来解决
xorFunc :: [Int] -> [Int] -> [Int]
xorFunc xs ys = zipWith xor xs ys
以及应用eta REDUCT(两次),即忽略在最后一个位置出现的参数,这些参数可由类型检查器完全导出
xorFunc :: [Int] -> [Int] -> [Int]
xorFunc = zipWith xor
中缀函数用标点符号拼写,可以用括号作为前缀;e、 g.
x+y
也可以拼写为(+)xy
。相反,前缀函数是用字母拼写的,可以用反勾号做成中缀;e、 g.zip-xs-ys
也可以拼写为xs`zip`ys
将其应用到您的案例中,这意味着您应该编写一个
xor xy
或x`xor`y
,而不是xor y
xor
是一个常规函数名,而不是运算符。您需要将其括在反引号中以用作中缀运算符
xorFunc xs ys = zipWith (\x y -> x `xor` y) xs ys
也就是说,不需要lambda表达式;只需使用xor
作为zip
的参数:
xorFunc xs ys = zipWith xor xs ys
或者干脆
xorFunc = zipWith xor
(同样地,
和func=zipWith(.&.)
;将运算符括在括号中,将其用作函数值。)谢谢你们回答我愚蠢的问题:$
xorFunc = zipWith xor