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 highlevelfunctio


From: WJ
Subject: Re: How to improve the readability of (any) LISP or any highlevelfunctional language to the level of FORTH ?
Date: 9 Jan 2011 10:41:05 GMT
User-agent: XanaNews/1.18.1.6

w_a_x_man wrote:

> On Jan 1, 5:56 pm, The Quiet Center <thequietcen...@gmail.com> wrote:
> 
> > How would you code this simple list compression problem in Ruby:
> > 
> > 1.08 (**) Eliminate consecutive duplicates of list elements.
> >     If a list contains repeated elements they should be replaced
> > with a single copy of the element. The order of the elements should
> > not be changed.
> > 
> >     Example:
> >     ?- compress([a,a,a,a,b,c,c,a,a,d,e,e,e,e],X).
> >     X = [a,b,c,a,d,e]
> 
> 
> 
> (defun compress (stuff)
>   (mapcon #'(lambda (x)
>               (if (and (> (length x) 1) (eql (car x) (cadr x)))
>                 nil
>                 (list (car x))))
>           stuff))

MAPCON works quite well for this.
Tweeked:

(defun compress (stuff)
  (mapcon #'(lambda (x)
              (if (and (rest x) (eql (first x) (second x)))
                nil
                (list (first x))))
          stuff))



reply via email to

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