Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print variables from inside a function?

Tags:

haskell

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?

like image 831
Anton Savelyev Avatar asked Sep 28 '16 19:09

Anton Savelyev


People also ask

How do you print a variable inside a function?

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.

How do you print the value of a function?

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.

Can you read global variables inside of a function?

Global variables can be used by everyone, both inside of functions and outside.


2 Answers

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.

like image 172
redneb Avatar answered Sep 25 '22 02:09

redneb


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.

like image 45
chepner Avatar answered Sep 25 '22 02:09

chepner