[Haskell-beginners] to avoid naming intermediate variables

Dennis Raddle dennis.raddle at gmail.com
Mon Feb 22 08:40:53 UTC 2016


I recently realized one important reason Haskell is so compact---it
provides structures that avoid the need to name intermediate variables. I
guess it's obvious, but coming from an imperative background I didn't see
this at first. I am starting to recognize situations where all the existing
wonderful typeclasses assist in eliminating variables.

Function composition is the simplest example. I sometimes write long chains
of composed functions. In an imperative language with a less compact
function call syntax, that would require so many parentheses that you would
probably break it over several statements, then forcing you to use more
variables.

Then I realized that Arrows allow a little more flexibility when you have
different numbers of arguments to feed, say when you split a single
argument into two, or want to construct a function that applies to one
argument and ignores the other.

...So I had to write something like this.

compute :: [Int] -> [Int] -> [Int] -> [(Int,Int)]

func :: [Int] -> [(Int,Int)]
func xs = compute xs (filter isSmall xs) (filter isLarge xs)


but I could also write

compute :: ([Int],([Int],[Int])) -> [(Int,Int)]

func = compute . (id &&& filter isSmall &&& filter isLarge)

So I had to change the inputs to 'compute' into this kind of awkward tuple
form, but I eliminated four 'xs', plus eliminated the need to come up with
the name 'xs'.

Can I get a comment on whether this makes sense as way to do things?

D
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160222/15f9a64a/attachment.html>


More information about the Beginners mailing list