我试图创建自己的字符串shuffle函数:
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有关,但我不知道如何解决它。
答案 0 :(得分:4)
首先,您没有将字符串参数传递给remove_char
和get_char
。此外,您需要将get_char
的结果转换为列表才能使用++
。对my_shuffle
的递归调用应如下所示:
my_shuffle (remove_char s pos) (result ++ [get_char s pos])
其次,您需要将{mon}用于randomIO
,因此my_shuffle
的签名应为:
my_shuffle :: [Char] -> [Char] -> IO [Char]
然后最后你需要在基本情况下使用return
(因为你需要返回IO [Char]
):
my_shuffle [] result = return result
修复已应用:
import System.Random
my_shuffle :: [Char] -> [Char] -> IO [Char]
my_shuffle [] result = return result
my_shuffle s result = do
pos <- randomRIO (1, length s)
my_shuffle (remove_char s pos) (result ++ [get_char s 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