[Top][All Lists]

[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: Jan Burse
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 02:10:45 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv: Gecko/20101123 SeaMonkey/2.0.11

The Quiet Center schrieb:

% Problem 1.08 - compress consecutive duplicates into a single
% list element

compress([], []).
compress([X,Y], [X,Y]) :- X \= Y.
compress([X,Y], [X])   :- X  = Y.

% problems writing other clauses (thanks RLa)

compress([X,Y|Z], [X|Z1])   :- X \= Y, compress([Y|Z], Z1).
compress([X,Y|Z], Z1)       :- X  = Y, compress([Y|Z], Z1).

The above code does not work fully correct. Here is a
counter example (try yourself):

    ?- compress([a],X).

In a Prolog introductory course I would anyway use:

    % compress(+List,-List)

    compress([X,X|L],R) :- !, compress([X|L],R).
    compress([X|L],[X|R]) :- compress(L,R).

No reason to use (\=)/2, which is anyway not declarative,
and no reason to avoid the cut !/0.


P.S.: On I find the following solution,
correct, but also avoids the cut !/0:

% 1.08 (**): Eliminate consecutive duplicates of list elements.

% compress(L1,L2) :- the list L2 is obtained from the list L1 by
%    compressing repeated occurrences of elements into a single copy
%    of the element.
%    (list,list) (+,?)

compress([X,X|Xs],Zs) :- compress([X|Xs],Zs).
compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).

reply via email to

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