我有一个[字符]:
*Main> let message = "something"
我已经改组了字符串(你可以在这里找到完整的代码):
*Main> let key = shuffle message
*Main> message
"something"
*Main> :t message
message :: [Char]
*Main> key
"goeimntsh"
*Main> :t key
key :: IO [Char]
现在我需要从这两个字符串创建 Data.Map 。就像是:
Map.fromList(zip message key)
但我什至不能压缩 [Char] 和 IO [Char]:
*Main> zip message key
<interactive>:1:13:
Couldn't match expected type `[b0]' with actual type `IO [Char]'
In the second argument of `zip', namely `key'
In the expression: zip message key
In an equation for `it': it = zip message key
我了解 shuffle 函数不会为相同的输入返回相同的结果。所以它必须返回 IO [Char]。
我了解我无法获取 Map.Map,并且我同意获取 IO Map.Map。但我需要像普通字符串一样使用 IO [Char]。我怎么才能得到它 ?
谢谢。
更新:
谢谢大家的解释。一些额外的问题:
@肯尼TM:
我理解 'do'- 表示法,但我需要一点时间来获得 liftM :) 但现在还有一个额外的问题:
*Main> let s = "abcdef"
*Main> let d = shuffle s
*Main> s
"abcdef"
*Main> d
"fedcba"
*Main> buildIOMap s d
fromList [('a','e'),('b','a'),('c','c'),('d','f'),('e','d'),('f','b')]
*Main> buildIOMap2 s d
fromList [('a','c'),('b','b'),('c','f'),('d','a'),('e','e'),('f','d')]
*Main> Map.fromList (zip "abcdef" "fedcba")
fromList [('a','f'),('b','e'),('c','d'),('d','c'),('e','b'),('f','a')]
'buildIOMap' 是一个 do-notation;'buildIOMap2' - 是一个 liftM - 实现。
为什么我在三种情况下得到不同的结果?