我有一个功能appendLetters :: [[Char]] -> [[Char]].当我尝试iterate像这样调用这个函数时:iterate appendLetters [""],ghci告诉我:
Couldn't match type '[Char]' with 'Char'
Expected type: [Char] -> [Char]
Actual type: [[Char]] -> [[Char]]
In the first argument of 'iterate', namely 'appendLetters'
In the second argument of 'genericTake', namely
'(iterate appendLetters [""])'
In the expression: genericTake n (iterate appendLetters [""])
Couldn't match expected type 'Char' with actual type `[Char]'
In the expression: ""
In the second argument of 'iterate', namely '[""]'
In the second argument of 'genericTake', namely
'(iterate appendLetters [""])'
Run Code Online (Sandbox Code Playgroud)
失败,模块加载:无.
为什么iterate期望有这些参数类型?我怎样才能使它工作?
提前致谢.
编辑:完整代码:
wordsOfLength :: [Char] -> Integer -> [[Char]]
wordsOfLength alphabet n = genericTake n ( iterate appendLetters [""] ) where appendLetters words = [ atFirst ++ [letter] | atFirst <- words , letter <- alphabet ]
Run Code Online (Sandbox Code Playgroud)
说明:wordsOfLength应采用字母表并在此字母表中创建长度为n的所有单词.这是一个家庭作业,我不想获得解决任务本身的帮助,但只能使用iterate函数.
表达方式
iterate appendLetters [""]
Run Code Online (Sandbox Code Playgroud)
有类型[[[Char]]](iterate :: (a -> a) -> a -> [a],在你的情况下a == [[Char]]).所以结果genericTake会有相同的类型.但是您的wordsOfLength函数具有输出类型[[Char]],这会导致类型不匹配.
直觉上,你返回一个列表(超过长度)的列表(可能的单词),其中单词是列表本身,所以它是[[[Char]]].
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |