Question

I am writing a function like this.

func :: IO()
func = putStr print "func = putStr print"

I know it is incorrect but the idea is I want the putStr applied onto the string then print applied onto the same string "fun = .." so that the output would be:

func = putStr print "func = putStr print"

which is the same as my function definition. Thanks

Was it helpful?

Solution

If your goal is to write a quine (Another Haskell example given in this article too), you could use lambda notation for variable capture.

func = (\x -> putStr x >> print x) "func = (\\x -> putStr x >> print x) "

OTHER TIPS

I'm not sure where you are going with the "without using >>" part (if that's really the point use do-notation), but you can easily write a helper function that applies two functions in sequence to the same input:

tee f g s = f s >> g s

func = tee putStr print "..."

Also, to just avoid repeating the string, a local variable with let or where would probably be the easiest:

let s = "..."
in  putStr s >> print s

You could do it like this:

doActions str actions = mapM_ ($ str) actions
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top