Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 简化xoring字符串_Haskell_Pointfree - Fatal编程技术网

Haskell 简化xoring字符串

Haskell 简化xoring字符串,haskell,pointfree,Haskell,Pointfree,我已将以下函数写入两个字符串xor import Data.Bits (xor) import Data.Char (ord, chr) -- xors strings of equal length stringXor :: String -> String -> String stringXor s t = map chr $ zipWith xor (f s) (f t) where f = map ord 看起来你至少不能拥有第一张地图,但我无法压缩合成chr。x

我已将以下函数写入两个字符串
xor

import Data.Bits (xor)
import Data.Char (ord, chr)

-- xors strings of equal length
stringXor :: String -> String -> String
stringXor s t = map chr $ zipWith xor (f s) (f t) where
    f = map ord
看起来你至少不能拥有第一张
地图
,但我无法压缩合成
chr。xor
开始工作

是否存在一些涉及
zipWith
map
的有用标识/公式


我是Haskell的新手,因此,如果没有太多无点魔法的简化,那就很高兴看到:)

好吧,您可以使用
上的
fmap来简化您的版本:

import Data.Function (on)

stringXor :: String -> String -> String
stringXor = zipWith (fmap chr . xor) `on` map ord
另外,
fmap-chr.
可以是
(chr.)。
,但我发现第一个更便于键入

编辑

您甚至可以将其简化为:

stringXor = zipWith (fmap chr . xor `on` ord)
现在我们已经消除了两个
map
s