Article View: comp.lang.functional
Article #1196Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ?
From: w_a_x_man
Date: Sun, 02 Jan 2011 07:14
Date: Sun, 02 Jan 2011 07:14
23 lines
2390 bytes
2390 bytes
On Jan 1, 11:36 pm, Nathan <nbeen...@gmail.com> wrote:
> Well, to answer the question simply, this code will provide the
> 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
Instead of x[x.size-1], use x[-1] or x.last.
> x + [y]
> else
> x
> end
>
> }
Another way using reduce:
%w(a a a b c c a a d e e e).reduce([]){|a,x| a[-1]==x ? a : a<<x}
==>["a", "b", "c", "a", "d", "e"]
This assumes nils aren't allowed in the source array.
Technical Details
Message-ID:
<8a75fa03-3728-43fb-9747-cc9c263605e8@f20g2000prn.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!f20g2000prn.googlegroups.com!not-for-mailReferences:
<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> <aa0e6de9-26c7-4a98-93af-24d0188a3506@m35g2000vbn.googlegroups.com> <3bd9f0cb-a400-4332-adf2-27cddb1a2db6@l7g2000vbv.googlegroups.com> <1347a932-5751-4c20-9c4a-5594ed860ea6@35g2000prb.googlegroups.com>Show Full Headers
Complete Article Headers
X-Google-Thread: ee3de009bc,1641de092071599d X-Google-Attributes: gidee3de009bc,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!f20g2000prn.googlegroups.com!not-for-mail From: w_a_x_man <w_a_x_man@yahoo.com> 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: Sat, 1 Jan 2011 23:14:47 -0800 (PST) Organization: http://groups.google.com Lines: 23 Message-ID: <8a75fa03-3728-43fb-9747-cc9c263605e8@f20g2000prn.googlegroups.com> 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> <aa0e6de9-26c7-4a98-93af-24d0188a3506@m35g2000vbn.googlegroups.com> <3bd9f0cb-a400-4332-adf2-27cddb1a2db6@l7g2000vbv.googlegroups.com> <1347a932-5751-4c20-9c4a-5594ed860ea6@35g2000prb.googlegroups.com> NNTP-Posting-Host: 209.42.179.244 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1293952488 12747 127.0.0.1 (2 Jan 2011 07:14:48 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 2 Jan 2011 07:14:48 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: f20g2000prn.googlegroups.com; posting-host=209.42.179.244; posting-account=vf5QpQoAAADgh9WOa2uZtoS1yoyJdEBw User-Agent: G2/1.0 X-HTTP-UserAgent: Opera/9.80 (Windows NT 5.1; U; en) Presto/2.6.30 Version/10.63,gzip(gfe)