Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.forth Subject: Re: :Re: My LOOP is ugly Date: Tue, 29 Nov 2016 23:31:10 -0800 Organization: A noiseless patient Spider Lines: 31 Message-ID: <87eg1t77b5.fsf@nightsong.com> References: <20161130063555.3d621465@maxa-pc> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="87eff38ad298ed4202aa26656d0a3ad1"; logging-data="4908"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180XRzwMYzjHT+QOEvBxnYI" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Z0oTPLHJMsn6K5BD1h/BaAF9jIM= sha1:J/N9okbV+lyznWFkTlYhNIhA9N4= Xref: csiph.com comp.lang.forth:52980 Melzzzzz writes: > Haskell: > .... > p2b = let f :: [(a,b)] -> (a,[b]) > f x = foldl (\(a,b) (_,y) -> (a,y:b)) (fst.head $ x,[snd.head $ x]) (tail x) > in map (\x->(fst $ f x,sort.snd $ f x)) > . groupBy (\x y-> fst x == fst y ) . sort > ... {-# LANGUAGE TransformListComp #-} import GHC.Exts (sortWith, groupWith, the) import Data.Char (toUpper) import Data.Function (on) p2b xs = [ (the a, b) | (a,b) <- xs , then sortWith by b , then group by a using groupWith ] t1 = [('C',3),('A',1),('B',2),('A',2),('C',2)] t2 = [("bar",44),("Foo",2),("foo",22),("BAR",4)] main = do print . p2b $ t1 print . p2b $ [(map toUpper a, b) | (a,b) <- t2] ================================================================ *Main Prelude> main [('A',[1,2]),('B',[2]),('C',[2,3])] [("BAR",[4,44]),("FOO",[2,22])]