[Haskell-cafe] Re: Practise fingerspelling with Haskell! (Code
cleanup request)
Dougal Stanton
ithika at gmail.com
Wed Jul 18 16:13:47 EDT 2007
On 18/07/07, apfelmus <apfelmus at quantentunnel.de> wrote:
> I like it for its elegant point-free style :)
Yes, well, I am rather enamoured of them! :-)
> Apparently, difference can only detect character replacements but not
> character insertion or deletion, but that's probably not your use case.
Yes, that is the case. If I allowed words differing in length they
would necessarily look different, so it would be less of a challenge.
I could still challenge people to identify the two words of course.
Any practice is good.
> You can avoid generating the superfluous half of the pairs by using tails
>
> listPairs ws = [ (head ws', w')
> | ws' <- tails ws, w' <- ws'
> , let w = head ws', length w == length w']
>
> Of course, grouping words by length first and pairing the resulting
> groups is more efficient than filtering out all the pairs where length
> w /= length w'. But you restrict fingerspell to a fixed word length
> anyway, so it doesn't matter.
I realised after I sent that post that I had *aready* filtered the
words so they were all the same length. So the length condition in
that list comprehension was completely superfluous. Meh.
I will look at using tails to clean things up a bit. I tried to see if
there were redundant parts I could remove today, but I was stymied by
my lack of understanding of the list comprehensions.
I worked out that [ (a,b) | a <- as, b <- bs ] must be equivalent to
> comp = concatMap (\x -> map ((,) x) ys) xs
but I can't really say how conditions like "a /= b" get slotted in to
that style. Is there a reference for that?
Cheers,
Dougal.
More information about the Haskell-Cafe
mailing list