1

我知道它很冗长,但这就是我学习语法的方式。这条线在

(abCombo' a 2 lst) ... 我想返回“列表”和/或打印列表,但我无法使用此返回类型“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)
4

1 回答 1

6

要在 -block 中绑定操作的结果,do您需要使用<-而不是let.

res <- abCombo' a 2 lst      -- res :: [Int]

这是因为使用let,您只是在动作本身上命名。

let res = abCombo' a 2 lst   -- res :: Writer [String] [Int]
于 2012-04-13T13:52:17.230 回答