I know it is verbose but that is how I am learning the syntax. With this line at
(abCombo’ a 2 lst) … I want to return the ‘list’ and/or print the list but I am having trouble extracting the list with this return type, ‘ 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
…
That is the current code above, I want to change it to:
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)
To bind the result of an action in a
do-block you need to use<-instead oflet.This is because with
let, you’re just putting a name on the action itself.