在字符串洗牌功能中输入错误 [英] Type error in string shuffle function

查看:161
本文介绍了在字符串洗牌功能中输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import System.Random 

我试图创建自己的字符串shuffle函数 - 用例:my_shufflesomething

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:
无法匹配预期类型实际类型为'IO a0'的$ [t0]'
在'randomRIO'调用的返回类型中
在'do'表达式的句柄中:pos < - randomRIO(1,length s)
在表达式中:
do {pos < - randomRIO(1,length s);
my_shuffle(remove_char pos)(result ++(get_char pos))}

看到它与IO有关,但我不知道如何解决它。

解决方案

首先,将字符串参数传递给 remove_char get_char 。此外,为了使用 ++ ,您需要将 get_char 的结果转换为列表。递归调用 my_shuffle 应该如下所示:

  my_shuffle(remove_char s第二,你需要使用IO monad(pos)(result ++ [get_char s pos])

对于 randomIO ,所以 my_shuffle 的签名应该是:

  my_shuffle :: [Char]  - > [Char]  - > IO [Char] 

然后最后你需要使用 return 在基本情况下(因为你需要返回一个 IO [Char] ):

  my_shuffle []结果=返回结果

应用修正:

  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)(结果++ [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


I have tried to create my own string shuffle function:

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

It returns the error message:

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)) }

As I see it is related to IO, but I don't know how to fix it.

解决方案

First of all, you aren't passing the string argument toremove_char and get_char. Also, you need to turn the result of get_char into a list in order to use ++. The recursive call to my_shuffle should look like:

my_shuffle (remove_char s pos) (result ++ [get_char s pos])

Secondly, you need to use the IO monad for randomIO, so the signature of my_shuffle should be:

my_shuffle :: [Char] -> [Char] -> IO [Char]

Then finally you need to use return in the base case (since you need to return an IO [Char]):

my_shuffle [] result = return result

With fixes applied:

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

这篇关于在字符串洗牌功能中输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆