Haskell xor不适用于映射

时间:2017-10-06 18:00:45

标签: function haskell xor

我在//This will fire every time an instance of datePickerTemplate is rendered. Template.datePickerTemplate.onRendered( () => { $('.datepicker').pickadate({ selectMonths: true, // Creates a dropdown to control month selectYears: 15, // Creates a dropdown of 15 years to control year, today: 'Today', clear: 'Clear', close: 'Ok', closeOnSelect: false // Close upon selecting a date, }); $('select').material_select(); }); 模块中使用xor函数时遇到问题 像下面的代码

Data.Bits

当我尝试使用import Data.Bits andFunc :: [Int] -> [Int] -> [Int] andFunc xs ys = zipWith (\x y -> x .&. y) xs ys xorFunc :: [Int] -> [Int] -> [Int] xorFunc xs ys = zipWith (\x y -> x xor y) xs ys andFunc的参数[1..10]时(参数只是任意数组)

它有效。 (不写在这里,但[2..11]也有效)

但是有些原因,orFunc (.|.)没有....并且说

xorFunc

你知道为什么吗?

运行环境:    GHC 8.2.1没有标志    Windows 10 64位

3 个答案:

答案 0 :(得分:6)

如果要在中缀表示法中使用函数,则必须使用反引号语法。

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 reduce(两次),即省略在最后位置发生的参数,并且可以由类型检查器完全导出。

xorFunc :: [Int] -> [Int] -> [Int]
xorFunc = zipWith xor

答案 1 :(得分:3)

中缀函数拼写有标点符号,可以用括号括起来;例如x + y也可以拼写为(+) x y。朝另一个方向,前缀函数拼写为字母,可以用反引号作为中缀;例如zip xs ys也可以拼写为xs `zip` ys

将其应用于您的案例,这意味着您应该写一个xor x yx `xor` y而不是x xor y

答案 2 :(得分:3)

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

(同样地,andFunc = zipWith (.&.);将运算符括在括号中以将其用作函数值。)