我试图创建自己的字符串洗牌功能:
import System.Random
-- usage case: my_shuffle "something" ""
my_shuffle :: [Char] -> [Char] -> [Char]
my_shuffle [] result = result
my_shuffle s result = do
pos <- randomRIO (1, length s)
my_shuffle (remove_char pos) (result ++ (get_char pos))
get_char :: [Char] -> Int -> Char
get_char s pos = s !! (pos - 1)
remove_char :: [Char] -> Int -> [Char]
remove_char s pos = take (pos - 1) s ++ drop pos s
它返回错误消息:
substitution_cipher.hs:8:16:
Couldn't match expected type `[t0]' with actual type `IO a0'
In the return type of a call of `randomRIO'
In a stmt of a 'do' expression: pos <- randomRIO (1, length s)
In the expression:
do { pos <- randomRIO (1, length s);
my_shuffle (remove_char pos) (result ++ (get_char pos)) }
我看到它与 IO 有关,但我不知道如何解决它。