News for hackers. Stuff that matters.

Ask HN: How would you do this in Haskell?

Say I have a complicated calculation, but it's pure functional all the way. Now somewhere in the middle of it, I want to log something if some condition is met. In a procedural language, that's easy - I just add an "if" and a log statement. But how would you do that in Haskell?

The best answer I've come up with so far is, from that function, to return a tuple containing two values: the value the function returned before, and a Maybe. The Maybe contains Just String if the logging happened, and Nothing otherwise.

But then all functions from there to the end of the calculation have to be changed to return a tuple instead of just the results of the calculation. That doesn't sound like composablility.

Or, I could jam IO into the function where the data was to decide whether to do the logging, but that just feels wrong.

What's the idiomatic way to do something like this?

( 11 comments | 7 points | on HN )
Re:Ask HN: How would you do this in Haskell? by paulrpotts on Wednesday April 22, 2015 @01:13PM (#9421891)
Mr. Muppet, I am not a Haskell expert, but I had a similar coding issue that I blogged about a while back. I was threading a score value through some code that calculates an updated gameboard. The solution (well, one solution) was a writer monad. That got me over the hump a little further towards understanding monads, and the conventional wisdom nowadays seems to be that teaching beginning Haskell developers how to _use_ them is more effective than pointing them at endless monad tutorials or articles on category theory.

If that sounds helpful, see: http://praisecurseandrecurse.blogspot.com/2013/07/the-polar-... and see also the next post in the blog. Then maybe go from there!

Re:Ask HN: How would you do this in Haskell? by AnimalMuppet on Wednesday April 22, 2015 @02:09PM (#9422310)
That's actually rather helpful, especially the "important stuff, but not the point" line. That's a bigger deal than my question.
Re:Ask HN: How would you do this in Haskell? by paulrpotts on Thursday April 23, 2015 @10:19AM (#9426812)
Glad to hear it. I think once you've used one of the monads the whole concept starts to make a lot more practical sense.
Re:Ask HN: How would you do this in Haskell? by Tyr42 on Wednesday April 22, 2015 @02:06PM (#9422290)
This is exactly where you might like to use the writer monad. Check out LYAH http://learnyouahaskell.com/for-a-few-monads-more

It's just a fancy name for composing functions of the form `a -> (b, String)`, via

    (>>=) :: (a, String) -> (a -> (b, String)) -> (b, String)
    (a, log1) >>= f = let (b, log2) = f a in
                      (b, log1 ++ log2).
Re:Ask HN: How would you do this in Haskell? by spimta on Wednesday April 22, 2015 @01:13PM (#9421889)
Echoing stonemetal, I would recommend seeing if you can refactor so that you can keep your pure function as-is, and then lift it into some monad that takes care of the logging. The Writer monad can be a good way to do this elegantly.
Re:Ask HN: How would you do this in Haskell? by AnimalMuppet on Wednesday April 22, 2015 @02:08PM (#9422301)
Yeah, I kind of suspected that this was going to involve a monad :-) And the Writer monad looks like the right one for the job.

But doesn't using that involve making the rest of the code (at least from the function that needs to log out to the top level) return monads, rather than returning whatever they were returning before?

Re:Ask HN: How would you do this in Haskell? by Tyr42 on Wednesday April 22, 2015 @03:25PM (#9422924)
So, it'd require making any code that calls the function doing the logging do one of two things:

1. Explicitly end the scope of implicitly logging things by calling runWriter, and getting a pair of (result, log) out.

2. Continue working in the implicit log monad, which would mostly involve extracting the result of the log function with `do x <- theLoggingThing`, but you can still mix in pure statments with `do let y = 5` or other function calls, and then end the do block with `return val`, where val was what you were going to return before.

Re:Ask HN: How would you do this in Haskell? by stonemetal on Wednesday April 22, 2015 @01:07PM (#9421859)
There is a problem with your premise. Why do you need to log in the middle of that function? In a procedural language you need to log right then to make sure state hasn't changed on you. In Haskell you could do it before or after the function and not be able to tell the difference because state can't change on you.
Re:Ask HN: How would you do this in Haskell? by AnimalMuppet on Wednesday April 22, 2015 @01:57PM (#9422209)
No, it's not a matter of the state changing. The premise is that, at each step of the calculation, a function does some computation and returns a result. At the point where the logging decision is being made, the decision is based on data available within the function, but not being returned by the function - only a derived value is returned.

Now, perhaps you could argue that the right way to do the problem (without the logging) in Haskell is to not have functions like that - to break up that function so that the data that, in my version, is only available within the function is instead explicitly returned by some smaller function.

But if you don't argue that, then you're left with the stated problem: What's the right way of getting logging out of the middle of a function in Haskell?

Re:Ask HN: How would you do this in Haskell? by chris_j on Wednesday April 22, 2015 @01:57PM (#9422211)
What if you want to see an intermediate value part way through a complex calculation?
Re:Ask HN: How would you do this in Haskell? by cptndarling on Wednesday April 22, 2015 @10:53PM (#9424776)