So I have a main function (foo) that recursively calls two other functions (step1 & step2). foo will add a1 to a2 a count
amount of times and then return (a1, a2). How can I print the variables count, a1, and a2 at each step?
-- adds a1 to a2 a `count` number of times
-- returns (a1, a2) once count reaches 0
foo :: Integer -> Integer -> Integer -> (Integer, Integer)
foo count a1 a2 | count == 0 = (a1,a2)
| otherwise = foo count' a1' a2'
where (count', a1', a2') = let (count'', a1'', a2'') = step1 count a1 a2
in step2 count'' a1'' a2''
-- adds a2 to a1. How to print out count, a1 and a2' here?
step1 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step1 count a1 a2 = (count, a1, a2')
where
a2' = a1 + a2
-- decrements count by 1. How to print out count', a1 and a2 here? Or can I do these prints somewhere in the `foo` function?
step2 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step2 count a1 a2 = (count', a1, a2)
where
count' = count - 1
This is a simplified version of code from a larger code base. I am open to using a different approach. The example output that I am looking for is:
$> foo 3 4 5
3 4 5
3 4 9
2 4 9
2 4 13
1 4 13
1 4 17
0 4 17
(4, 17)
EDIT: I just realized I could probably store intermediary results in a list and then print from that list. But am I correct to think that I would have to pass the list as an argument to the functions?
You first include the character f before the opening and closing quotation marks, inside the print() function. To print a variable with a string in one line, you again include the character f in the same place – right before the quotation marks.
To print a value in Python, you call the print() function. After printing a value, you can no longer use it. Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword.
Global variables can be used by everyone, both inside of functions and outside.
You have to change foo
and make it operate in the IO
monad. Effectively this "tags" the function as being impure (i.e. it has side effect, such as printing on stdout
) which allows it to call functions such as print
. Here's an example:
foo :: Integer -> Integer -> Integer -> IO (Integer, Integer)
foo count a1 a2 = do
print (count, a1, a2)
case count of
0 -> do
print (a1,a2)
return (a1,a2)
_ -> do
let (count'', a1'', a2'') = step1 count a1 a2
(count', a1', a2') = step2 count'' a1'' a2''
foo count' a1' a2'
Note: If you want to print these values for debugging purposes, then you can use Debug.Trace
as shown in chepner's answer. You should do that for debugging purposes only and for no other reason.
For debugging purposes only, you can use Debug.Trace
. For example:
import Debug.Trace
-- adds a2 to a1. How to print out count, a1 and a2' here?
step1 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step1 count a1 a2 = traceShowID (count, a1, a2')
where
a2' = a1 + a2
-- decrements count by 1. How to print out count', a1 and a2 here? Or can I do these prints somewhere in the `foo` function?
step2 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step2 count a1 a2 = traceShowID (count', a1, a2)
where
count' = count - 1
traceShowID :: Show a => a -> a
is basically id
with the (unannounced) side effect of also printing the argument's string representation according to show
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With