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: Frank GOENNINGER
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 15:07:02 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin)

The Quiet Center <thequietcenter@gmail.com> writes:

> On Jan 1, 11:08 am, Nathan <nbeen...@gmail.com> wrote:
>> If you want a easy to read self-documenting functional language, look
>> into Ruby. I know personally that Ruby syntax was a big turn off to me
>> for several weeks (kind of like Lisp) but once you learn it, it
>> becomes the easiest to read of any programming language I've ever
>> experimented with. No contest.
>>
>
> Well, Python was chosen over Ruby for MIT's rework of their intro to
> cs course because Python is multi-paradigm, whereas Ruby claims
> everything is an object.
>
> 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]
>
>> Matz himself admitted that “...Ruby is a bad rip-off of Lisp... But it
>> is nicer to ordinary people.”
>
> yeah I guess the LOOP macro is where I got stuck in doing Lisp.

Simple:

? (defun compress (list)
  (let ((last-element)
        (result))
    (loop for element in list 
       when (not (equal last-element element))
       do
         (progn
           (setq last-element element)
           (push element result)))
    (reverse result)))

COMPRESS

? (setq list (list 'a 'a 'a 'a 'b 'c 'c 'a 'a 'd 'e 'e 'e 'e))
(A A A A B C C A A D E E E E)

? (compress list)
(A B C A D E)

'compress can be done in much more terse way in Lisp but this is just an
example using the Loop macro.

Frank


reply via email to

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