我有一个功能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 [""])'
失败,加载模块:无。
为什么iterate
期望有这些参数类型?我怎样才能让它工作?
提前致谢。
编辑:完整代码:
wordsOfLength :: [Char] -> Integer -> [[Char]]
wordsOfLength alphabet n = genericTake n ( iterate appendLetters [""] ) where appendLetters words = [ atFirst ++ [letter] | atFirst <- words , letter <- alphabet ]
说明:wordsOfLength 应该采用一个字母表并在这个字母表上创建所有长度为 n 的单词。这是一项家庭作业,我不想获得解决任务本身的帮助,而只能使用迭代功能。