Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
List 如何在Haskell中将mod应用于列表中的每个元素_List_Haskell_Mod - Fatal编程技术网

List 如何在Haskell中将mod应用于列表中的每个元素

List 如何在Haskell中将mod应用于列表中的每个元素,list,haskell,mod,List,Haskell,Mod,我有一个函数,它生成一个整数列表,我想对其应用mod,这样列表中的任何元素都不会大于25。例如,列表[6,8,18,28,14,25]应返回为[6,8,18,2,14,25]。目前的守则如下 let charIntList = zip ['a'..'z'] [0..25] let getIntFromList (x:xs) a = if fst x == a then snd x else getIntFromList xs a charToInt :: Char -> Int let c

我有一个函数,它生成一个整数列表,我想对其应用mod,这样列表中的任何元素都不会大于25。例如,列表[6,8,18,28,14,25]应返回为[6,8,18,2,14,25]。目前的守则如下

let charIntList = zip ['a'..'z'] [0..25]
let getIntFromList (x:xs) a = if fst x == a then snd x else getIntFromList xs a
charToInt :: Char -> Int
let charToInt a = getIntFromList charIntList a
zipWithPosition :: [Char] -> [(Char, Int)]
let zipWithPosition m = zip m [0..length m]
position :: [(Char, Int)] -> [Int]
let position m = map snd (zipWithPosition m)
messToInt :: [Char] -> [Int]
let messToInt m = map charToInt m
almostVig :: [Int] [Int] -> [Int]
let almostVig m = zipWith (+) (messToInt m) (position m)
adjust :: [Int] -> [Int]
let adjust m = (mod (almostVig m) 26)

此代码无法将mod应用于almostVig生成的列表中的每个元素。我曾尝试在
adjust
中使用zipWith作为
let adjust m=zipWith(mod(almostVig m)26)
,但也失败了。如何将
mod
应用于列表中的每个元素,以生成一个没有元素大于25的新列表?

使用列表理解

[x `mod` 26 | x <- almostVig]
[x`mod`26 | x您可以使用:

map
的类型为
(a->b)->[a]->[b]
:它接受一个函数并将其应用于列表元素


`mod`26
是部分函数应用程序:它将函数
mod
转换为运算符
`mod`
,以便可以内联使用,如
128`mod`26
而不是
mod 128 26
,然后部分应用第二个参数。

什么是
(Char,Int)
[内部]
?感谢您添加签名,但这些没有意义。请编写一个实际的Haskell模块进行打字检查。不要添加与问题无关的内容。对不起,我是新手,自学成才。这是我最好的猜测。值得解释操作员部分。谢谢!我曾尝试使用map,但作为“让我调整m=map mod”(almostVig m)26'.@LillianWangler
map
只接受2个参数,而不是3个参数,这就是为什么这样编写时它不起作用的原因。
let adjust m = map (`mod` 26) (almostVig m)