🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #1189

Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?

#1189
From: Jan Burse
Date: Sun, 02 Jan 2011 01:10
49 lines
3420 bytes
The Quiet Center schrieb:
> %http://sites.google.com/site/prologsite/prolog-problems/1
>
> % 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).
     No

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).
     compress([],[]).

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

Bye

P.S.: On sites.google.com 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([],[]).
compress([X],[X]).
compress([X,X|Xs],Zs) :- compress([X|Xs],Zs).
compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).
Technical Details
Message-ID: <ifojao$73v$1@news.albasani.net>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!weretis.net!feeder4.news.weretis.net!news.albasani.net!not-for-mail
References: <80ceeca0-1d32-47d1-ba96-feb4d9729c3a@v17g2000yqv.googlegroups.com> <87pqsgk8v9.fsf@kuiper.lan.informatimago.com> <ca15d8c3-86db-46fd-b35f-1a2596475769@n32g2000pre.googlegroups.com> <9e7a7683-a92a-4e88-a6f1-9e6a6bd2f057@z9g2000yqz.googlegroups.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 107f24,1641de092071599d
X-Google-NewGroupId: yes
X-Google-Thread: f4fd2,1641de092071599d
X-Google-NewGroupId: yes
X-Google-Thread: 103b8b,1641de092071599d
X-Google-NewGroupId: yes
X-Google-Thread: fd7c9,1641de092071599d
X-Google-NewGroupId: yes
X-Google-Thread: f907c,1641de092071599d
X-Google-NewGroupId: yes
X-Google-Attributes: gid4e63bbc566,gidf85ca024c1,gid39ae015015,gid2832dde05d,gidee3de009bc,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!weretis.net!feeder4.news.weretis.net!news.albasani.net!not-for-mail
From: Jan Burse <janburse@fastmail.fm>
Newsgroups: comp.lang.functional,comp.lang.lisp,gnu.emacs.help,comp.lang.forth,comp.lang.prolog
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
Organization: albasani.net
Lines: 50
Message-ID: <ifojao$73v$1@news.albasani.net>
References: <80ceeca0-1d32-47d1-ba96-feb4d9729c3a@v17g2000yqv.googlegroups.com> <87pqsgk8v9.fsf@kuiper.lan.informatimago.com> <ca15d8c3-86db-46fd-b35f-1a2596475769@n32g2000pre.googlegroups.com> <9e7a7683-a92a-4e88-a6f1-9e6a6bd2f057@z9g2000yqz.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.albasani.net fDiUUzjp6AiqsDiMEfC/lb4CBE6lnWvkGw1Si0JwU76SrdN3FizTjwg3EQoZ6Q256QGLFojjUaLSUibv176qfg==
NNTP-Posting-Date: Sun, 2 Jan 2011 01:10:48 +0000 (UTC)
In-Reply-To: <9e7a7683-a92a-4e88-a6f1-9e6a6bd2f057@z9g2000yqz.googlegroups.com>
Cancel-Lock: sha1:SDkfVHtGzqKvrQp0Yqlgc34zTpE=
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.1.16) Gecko/20101123 SeaMonkey/2.0.11
Injection-Info: news.albasani.net; logging-data="i8V462ZsXj8zaGFR94jBSxXlMRwk/Kx7HreAlrQ6QaR3j8bOReDWjjhMr+foytqodsfGwu99S1RHyMJd+2ycl5tNiQQodWx93/NdJeWYzPQKdv+MM2vm1VH2U2dsVVoB"; mail-complaints-to="abuse@albasani.net"