haskell - 用所有字母字符Haskell替换字符串中的所有字符的函数

给一个字符串,我需要一个函数,该函数返回一个字符串列表,该列表是通过将原始字符串中的每个字符替换为字母中的每个小字母而形成的。例如"car"-> ["aar","bar","dar",....,"cbr","ccr",...]
我尝试了这个:

replaced1 :: String -> [String]
replaced1 [] = []
replaced1 (x:xs) = [h ['a'..'z'] : map (x:) (replaced1 xs)
         where h[] = []
               h c= head c  (h(tail c))

但是它在where上给了我一个解析错误。我只是在学习Haskell,所以我不习惯这种错误。我该如何解决?

最佳答案

一些东西。首先,您似乎正在考虑将列表理解与[h ['a'..'z']...一起使用试图将字符串的第一个字符替换为['a' .. 'z']系列中的每个字符。语法是[h c | c <- ['a'..'z']]。在评论中询问(或修改您的帖子)是否需要澄清,或者这不是您想的。

其次,h的类型应为Char -> String。它需要替换字符,然后将其余的输入用于replaced1(即xs)。

第三,要合并列表,请使用(++)而不是:map (x:) (replaced1 xs)的类型为[String],但添加类型的类型也是如此。

这是固定版本,还需要您填写一点。

replaced1 :: String -> [String]
replaced1 [] = []
replaced1 (x:xs) = [h c | c <- ['a'..'z']] ++ map (x:) (replaced1 xs)
         where h :: Char -> String
               h c = {- left as exercise -}

https://stackoverflow.com/questions/26950714/

相关文章:

compiler-errors - 编译示例Scalapack应用程序时出现问题

objective-c - 隐式转换为NSIndexPath的错误

compiler-errors - 无法解决错误,VHDL,语法错误,但我看不到

haskell - 应用函数两次编译错误Haskell

intellij-idea - IntelliJ : “java connot find symbo

c# - 抑制ASPX文件的生成错误

c#-4.0 - 为什么我的变量在分配时说未分配?

maven - 执行失败 : 'control build' & Failed to execute

compiler-errors - opencl编译错误

android - 关于主要 Activity : 'illegal start of expres