[Haskell-cafe] get a string representation (show) of a function argument

TP paratribulations at free.fr
Sun Apr 1 00:29:15 CEST 2012


I don't known the advanced features and extensions of GHC at all.

Look at the following program:
f :: Integer -> Integer -> IO Integer
f a b = do
    print $ "first argument=" ++ (show a)
    print $ "second argument=" ++ (show b)
    print $ a+b
    return (a+b)

main = do
    k <- f 3 5
    f 2 k

It yields:
"first argument=3"
"second argument=5"
"first argument=2"
"second argument=8"

I am wondering if there is any means to get "f 3 5" instead of "8" in the 
output of this program.
My idea would be to write some generic test function: I give it an expression 
(probably a function call) and the expected result (as a string), and then:

* it should check that the obtained result is identical to the expected one.

* it should also print the expression that yielded the first argument of the 
test function (i.e. the function call), under an understandable form (show). 
This is the feature that is missing to my mind in some test suites: for 
example in HUnit, it is not automatic: in the first argument of assertEqual, we 
may give a string corresponding to the executed command, see the documentation 
of Test.HUnit:

test1 = TestCase (assertEqual "for (foo 3)," (1,2) (foo 3))
> runTestTT tests
    # Failure in: 0:test1
    for (foo 3),
    expected: (1,2)
     but got: (1,3)
    Cases: 2  Tried: 2  Errors: 0  Failures: 1
So, we have duplication of code: "foo 3".
On the other hand, I guess that the needed features to do that are not 
possible in Haskell (related to some kind of metaprogramming), but I prefer to 
ask people about that.

Thanks in advance,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120401/dcf6b53e/attachment.htm>

More information about the Haskell-Cafe mailing list