[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-beginners
Subject:    Re: [Haskell-beginners] filter by max length a list of lists with equivalent values
From:       James Toll <james () jtoll ! com>
Date:       2014-02-11 15:34:31
Message-ID: C53400E1-FAA5-4D5B-BCB4-2EF596DB31C6 () jtoll ! com
[Download RAW message or body]

On Feb 11, 2014, at 9:08 AM, Frerich Raabe <raabe@froglogic.com> wrote:
> 
> After grouping the given list, so that you have
> 
> [[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[2,2],[7]]
> 
> Sort the list by comparing the first element of each list
> 
> ghci> sortBy (comparing head) [[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[2,2],[7]]
> [[1,1,1,1],[2,2,2,2],[2,2,2],[2,2],[3,3],[5],[6],[7]]
> 
> Then, group that again such that lists with equal elements get put into one list:
> 
> ghci> groupBy ((==) `on` head) \
> [[1,1,1,1],[2,2,2,2],[2,2,2],[2,2],[3,3],[5],[6],[7]] \
> [[[1,1,1,1]],[[2,2,2,2],[2,2,2],[2,2]],[[3,3]],[[5]],[[6]],[[7]]] 
> Finally, select the "maximum" of each inner list by comparing the length of the \
> sub-sub-lists: 
> ghci> map (maximumBy (comparing length)) \
> [[[1,1,1,1]],[[2,2,2,2],[2,2,2],[2,2]],[[3,3]],[[5]],[[6]],[[7]]]


Thank you for the nicely detailed explanation.  In my frustration, I was fearing I \
was going to have to revert to a more imperative approach to solving this, and I was \
hoping someone would demonstrate a more functional solution.  In the various \
iterations that I unsuccessfully tried, I think the major idea I was lacking was your \
second step, the groupBy ((==) `on` head).  That’s really the part I just wasn’t able \
to come up with on my own.

Again, thanks so much for the help.

Best,


James

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic