help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to improve the readability of (any) LISP or any highlevel functi


From: Paul Rubin
Subject: Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?
Date: Sun, 02 Jan 2011 13:21:54 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

LanX <lanx.perl@googlemail.com> writes:
>>    main = print [head xs | xs <- group my_list]
>
> does this really produce the desired output?
> Will the second 1 really be printed?

Yes.  "group" is a very useful function, that transforms a list into a
list-of-lists which collapses identical elements:

    group [1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5]

is

    [[1,1,1,1], [2], [3,3], [1,1], [4], [5,5,5,5]]

The list comprehension 

     [head xs | xs <- group my_list]

simply collects the first element of each of those sub-lists.

There is also groupBy, which lets you supply your own equality
predicate.  If there's not already a Lisp library similar to Python's
itertools module and Haskell's List module, it's probably worth writing
one.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]