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: Sat, 01 Jan 2011 22:46:08 -0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Nathan <nbeenken@gmail.com> writes:
> functionality you've been asking about in Ruby.
>
> my_list = [1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5]
> print my_list.reduce([]){|x, y|
>   if x.empty? or x[x.length-1] != y then
>     x + [y]
>   else
>     x
>   end
> }

That is pretty ugly; in Haskell you could write

   my_list = [1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5]
   main = print [head xs | xs <- group my_list]

That uses a Python-like list comprehension since you mentioned Python,
but more idiomatic would be

   my_list = [1, 1, 1, 1, 2, 3, 3, 1, 1, 4, 5, 5, 5, 5]
   main = print . map head . group $ my_list

There is probably a function like "group" available in Ruby.  Python has
itertools.groupby but it's a little bit brittle.


reply via email to

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