Ben*_*ore 2 haskell functional-programming list
我正在尝试更改IntHaskell 中的s 列表以使它们保持在特定限制内,但它似乎不起作用.我试图让列表中的每个int都在32到127之间,但它不起作用,任何人都可以解释为什么这不起作用?
limit :: Int -> Int
limit n | n > 127 = n `mod` 127 + 32
| n < 32 = n + 127 - 32
| otherwise = n
limitList :: [Int] -> [Int]
limitList [] = []
limitList (x:xs) = [limit x] ++ limitList xs
Run Code Online (Sandbox Code Playgroud)
根据您的注释,您希望通过应用模数转换来转换不在 32-127范围内的Ints .因此我们可以先实现一个功能:helper
helper x = 32 + mod (x-32) (128-32)
Run Code Online (Sandbox Code Playgroud)
这导致:
Prelude> helper 31
127
Prelude> helper 32
32
Prelude> helper 127
127
Prelude> helper 128
32
Run Code Online (Sandbox Code Playgroud)
接下来我们的函数limitList只是map与该帮助器的ping:
limitList = map helper
where helper x = 32 + mod (x-32) (128-32)
Run Code Online (Sandbox Code Playgroud)
这会产生:
Prelude> limitList [1,4..256]
[97,100,103,106,109,112,115,118,121,124,127,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,118,121,124,127,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82,85,88,91,94,97,100,103,106,109,112,115,118,121,124,127,34,37,40,43,46,49,52,55,58,61,64]
Run Code Online (Sandbox Code Playgroud)