[Haskell-beginners] best way to code this~~

Alex Hammel ahammel87 at gmail.com
Tue Jun 2 15:49:06 UTC 2015


Thank you so much for all the info !  Really appreciate it.
>

My pleasure!

however, i'm unclear why you didn't just use
>
>   eithers = map (\x -> lookupEither (uppercase x) assocs) xs
>
> instead of mapping everything to uppercase first.
>

Two reasons:

1) I thought it would be slightly more readable.
2) There's no performance penalty.

My version looks like it traverses the list twice, but it doesn't because
laziness. Where the compiler for a strict language might make an
intermediate, uppercased list, Haskell will produce the uppercase values as
they are needed.

To prove that to myself, I ran a quick criterion
<https://hackage.haskell.org/package/criterion> benchmark. If anything,
inlining the call to uppercase *decreases* performance slightly.

Incidentally, that's the same reason why the `filterMap` function you asked
about earlier doesn't exist. You can just do `(map f . filter p) xs`. The
values will be created lazily, with no intermediate list.


>
> meanwhile i need to get with the list comprehension program.  i use python
> list comprehensions all the time, and yet i continue to use map in haskell.
> how weird is that ?
>

Not super weird. In my experience listcomps (and their relatives) are much
more common in idiomatic Python than idiomatic Haskell.

Still something you'll want to know how to do, though. Think '|' = 'for',
'<-' = 'in' and ',' = 'if' and you'll be fine.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150602/94c3af5d/attachment.html>


More information about the Beginners mailing list