sampwing
sampwing

Reputation: 1268

How to call a function and return a different value in haskell? (W/o monads)

I am having issues figuring out if this is possible. Any pointers would be awesome

I'm not sure on the exact syntax but something like

m = 3
d = putStr "d"
f = d ; m

Such that it would call function d, and return the value in m without being Maybe m?

EDIT:

What I am looking for is more like this?

eval s = s
m = 3
f = (eval s) ; m

Upvotes: 2

Views: 226

Answers (2)

Mischa Arefiev
Mischa Arefiev

Reputation: 5417

If you want to debug your program in printf-like manner, use the Debug.Trace module.

Otherwise see Clark Gaebel's reply above — you can't¹ get pure (non-IO) values out of an IO function, and putStr is only possible in IO functions.


¹ actually sometimes you can, but it's a complex matter

Upvotes: 3

Clark Gaebel
Clark Gaebel

Reputation: 17948

If you call a function wrapped in a monad (IO in this case), then you must also be in the IO monad. Thus are the rules of monads - never to be broken.

m :: Int
m = 3

d :: IO ()
d = putStr "d"

f :: IO Int
f = do d -- Teehee, doodie.
       return m

Upvotes: 6

Related Questions