我知道它很冗长,但这就是我学习语法的方法。这条线在
(abCombo'a 2 lst)...我想返回'list'和/或打印列表但我无法使用此返回类型'Writer [String] [Int]'提取列表。
-- Loop through several integer values
-- and calculate the power of a^b, append to list
abCombo' :: Int -> Int -> [Int] -> Writer [String] [Int]
abCombo' a b lst
| b == maxB = do
tell [ " ... x-Done(1): a^b = " ++ show (a^b) ++ " // " ++ show lst ]
return ((a^b):lst)
| otherwise = do
tell [ " ... x-Processing: a^b = " ++ show (a^b) ++ " // " ++ show lst ]
abCombo' a (b+1) ((a^b):lst)
-- Loop through several integer values
-- and calculate the power of a^b, append to list
abCombo :: Int -> [Int] -> Writer [String] [Int]
abCombo a lst
| a == maxA = do
tell [ "- Done(2): a=" ++ show a ]
abCombo' a 2 lst
| otherwise = do
(abCombo' a 2 lst) <<<<<<<<<<<<<<<<<<<<<< line of interest, here
tell ["- Processing: a=" ++ show a]
abCombo (a + 1) lst
...
这是上面的当前代码,我想将其更改为:
abCombo :: Int -> [Int] -> Writer [String] [Int]
abCombo a lst
| a == maxA = do
tell [ "- Done(2): a=" ++ show a ]
abCombo' a 2 lst
| otherwise = do
let res = (abCombo' a 2 lst) <<<<<<<<<<<<<<<<<<<<<< line of interest, here
tell ["- Processing: a=" ++ show a]
abCombo (a + 1) (flatten snd res)
答案 0 :(得分:6)
要在do
- 块中绑定操作的结果,您需要使用<-
而不是let
。
res <- abCombo' a 2 lst -- res :: [Int]
这是因为使用let
,你只需在动作本身上加上一个名字。
let res = abCombo' a 2 lst -- res :: Writer [String] [Int]