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

2012-03-31 Thread TP
Hi,

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
8
first argument=2
second argument=8
10
10


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,

TP___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2012-03-31 Thread Roman Cheplyaka
* TP paratribulati...@free.fr [2012-04-01 00:29:15+0200]
 I am wondering if there is any means to get f 3 5 instead of 8 in the 
 output of this program.

No, this is not possible by referential transparency. The output of your
function can't depend on whether it is passed 8 or the result of f 3 5.

In Lisp-like languages, you could do what you want by using macros.
However, they have their own problems: you have to remember whether
something is a function or a macro, and what quoting it expects. For
instance, have a look at this StackOverflow question (and the answers):
http://stackoverflow.com/questions/9912511/filenotfoundexception-when-loading-my-project-core-with-an-external-dependency

In Haskell you can achieve your goal by using quasiquoting, but, unlike
in Lisp, it won't look like an ordinary function invocation (because it
isn't).

-- 
Roman I. Cheplyaka :: http://ro-che.info/

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe