4 ms·
I think you will get over this after about 3 days of using Haskell for real. It's an essential technique that greatly improves readability once you get used to
by jrockway 17y ago
I think you will get over this after about 3 days of using Haskell for real. It's an essential technique that greatly improves readability once you get used to it. Remember, functional programming is about composing functions (and effects), not about data flowing through the program. Ideally you will never see your data, but sometimes it's easier to mention it. (I thought the same thing; "I hate point-free". But then I started writing serious code, and realized that it's much easier to read when you don't mention the irrelevant.)
You have saved a total of 4 keystrokes. And made that line of code look really weird and hard to understand.
Anyway, your examples are faulty. Here's what they would look like in real life:
Point-ful:
sum :: Num a => [a] -> a
sum xs = foldr (+) 0 xs
Point-free:
sum :: Num a => [a] -> a
sum = foldr (+) 0
So there is no confusion; there is actually less confusion. Sum is just a fold over (+) with a seed of 0. That's all it is; the fact that there is this "xs" variable is irrelevant -- we know a fold operates on a list already, there is no need to say that again.
If the sum example confuses you, that's fine, that's not the best example of point-free style. Perhaps these are easier to understand:
Tack on ".xml" to every filename in a list:
(++ ".xml") <$> files
instead of:
(\x -> x ++ ".xml") <$> files
Extract the `snd` part of a value during a monadic computation:
f >>= return . snd
Instead of
f >>= \x -> return $ snd x
And so on. Basically, point-free style lets you omit the parts that are understood. In the second example, of course you're operating on the value that >>= binds. That's the point of >>=. No need to say that every time.
So yeah, I think point free style is one of these annoying features of Haskell, that seem to be put in so that users of Haskell can pat themselves on the back and feel like they are very smart. But in the end it does not add much to the language and makes it much less accessible.
Nope, sorry. Point-free notation often makes code simpler and easier to understand (and easier to compose, of course). If it doesn't make your particular code easier to read, then the solution is simple -- don't use it in that case. But it is yet-another useful tool in the toolbox.
- Groxx 17y agoNice examples, I was wondering about this myself (having not done any functional programming). Any suggestions for starting a functional language? And which to start with? I'd love to eventually get really deep into it, but I'm really busy lately and can't devote a lot of time to get my brain over a high hurdle.