From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 106387 invoked by alias); 28 Mar 2017 14:21:25 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 106352 invoked by uid 89); 28 Mar 2017 14:21:24 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: Yes, score=5.1 required=5.0 tests=AWL,BAYES_00,FOREIGN_BODY1,KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2 spammy=avez, dropbox, vous, tuesday X-HELO: smtps-n.oca.eu Received: from smtps-n.oca.eu (HELO smtps-n.oca.eu) (192.54.174.167) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 28 Mar 2017 14:21:14 +0000 Received: from [192.168.109.88] (natoca100-13.unice.fr [134.59.100.13]) by smtps-n.oca.eu (Postfix) with ESMTPSA id 416BF80517 for ; Tue, 28 Mar 2017 16:21:13 +0200 (CEST) From: Damien MATTEI To: kawa@sourceware.org Subject: Re: StackOverflowError in a specialized map Date: Tue, 28 Mar 2017 14:21:00 -0000 User-Agent: KMail/1.9.6 References: <25387.1490482561@vereq.eip10.org> <201703281042.25649.Damien.Mattei@unice.fr> In-Reply-To: <201703281042.25649.Damien.Mattei@unice.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <201703281621.13020.Damien.Mattei@unice.fr> X-IsSubscribed: yes X-SW-Source: 2017-q1/txt/msg00098.txt.bz2 i removed the nested _let_ ,the only thing that make crash the stack are th= e _cons_ that grow at each recursive call=20 for some explanation see: https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.1 Damien Le Tuesday 28 March 2017 10:42:25 Damien MATTEI, vous avez =E9crit=A0: > Le Saturday 25 March 2017 23:56:01 Sudarshan S Chawathe, vous avez =E9cri= t=A0: > > > From: Damien MATTEI > > > Date: Tue, 21 Mar 2017 15:00:57 +0100 > > >=20 > > > yes, thank you, but i try to stay in a functional programming style > > > and avo=3D id "loop" that make code unreadable and hard to debug, > >=20 > > I suspect I may be misunderstanding your quest, but I don't see a > > significant difference between a 'named let' and a nested function > > definition (with respect to being functional programming). Perhaps I > > should have used a name 'recur' instead of 'loop' in my earlier version. > >=20 > > For example, if we wish to avoid named let altogether for some reason, > > we could use the following variant of my earlier implementation. > >=20 > > (define (map/remove-nulls-1 proc . lsts) > > (define (f lsts result) > > (if (any null? lsts) > > (reverse result) > > (f (map cdr lsts) > > (let ((proc-result (apply proc > > (map car lsts)))) > > (if (null? proc-result) > > result > > (cons proc-result > > result)))))) > > (f lsts '())) > >=20 > > Or, going another way, we could use fold: > >=20 > > (define (map/remove-nulls-2 proc . lsts) > > (reverse (apply fold > > (lambda fargs > > (let ((accum (last fargs)) > > (pargs (drop-right fargs 1))) > > (let ((r (apply proc pargs))) > > (if (null? r) > > accum > > (cons r accum))))) > > '() > > lsts))) > >=20 > > As before, I'm using some SRFI 1 procedures for convenience; they can be > > avoided easily: any, fold, last, drop-right.=20=20 > >=20 > > Both the above versions, like the one I posted earlier, work without > > problems in Kawa without needing the --full-tailcalls option (i.e., Kawa > > properly detects and eliminates and simple cases of tail calls they > > use). The slight awkwardness of last/drop-right is due to the > >=20 > > But, as I noted before, perhaps I'm missing the main point of the > > original question. > >=20 > > Regards, > >=20 > > -chaw > >=20 > >=20 > >=20 >=20 > i have tested your solution (just used some? in place of any): > ;; (map/remove-nulls-1 (lambda (a b) (if (=3D a 2) '() (+ a b))) '(1 2 3)= '(4 5 6)) -> '(5 9) > (define (map/remove-nulls-1 proc . lsts) > (define (f lsts result) > (if (some? #;any null? lsts) > (reverse result) > (f (map cdr lsts) > (let ((proc-result (apply proc > (map car lsts)))) > (if (null? proc-result) > result > (cons proc-result > result)))))) > (f lsts '())) >=20 > and it worked with Kawa without the need of --fulltail-calls option: > [mattei@moita ~]$ kawa > #|kawa:1|# (require 'regex) > #|kawa:2|# (include-relative "../git/LOGIKI/lib/first-and-rest.scm") > /dev/stdin:2:20: cannot open file "../git/LOGIKI/lib/first-and-rest.scm" > #|kawa:3|# (exit) > [mattei@moita ~]$ cd Dropbox/Jkawa/ > [mattei@moita Jkawa]$ kawa > #|kawa:1|# (require 'regex) > #|kawa:2|# (include-relative "../git/LOGIKI/lib/first-and-rest.scm") > #|kawa:3|# (include-relative "../git/LOGIKI/lib/syntactic-sugar.scm") > #|kawa:4|# (include-relative "../git/LOGIKI/lib/display.scm") > #|kawa:5|# (include-relative "../git/LOGIKI/lib/case.scm") > #|kawa:6|# > #|kawa:7|# (include-relative "../git/LOGIKI/lib/list.scm") > #|kawa:8|# (include-relative "../git/LOGIKI/lib/map.scm") ;; for map-nil* > /home/mattei/Dropbox/Jkawa/../git/LOGIKI/lib/map.scm:671:9: warning - no = declaration seen for some? > #|kawa:9|# (include-relative "../git/LOGIKI/lib/set.scm") > /home/mattei/Dropbox/Jkawa/../git/LOGIKI/lib/set.scm:131:3: warning - no = declaration seen for andmap > #|kawa:10|# > #|kawa:11|# (define wds-url "http://ad.usno.navy.mil/wds/Webtextfiles/wds= newref.txt") > #|kawa:12|# (define wds-data-str &<{&[wds-url]}) > #|kawa:13|# (define wds-data-str-split (regex-split (string #\linefeed) w= ds-data-str)) > #|kawa:14|# (length wds-data-str-split) > 22569 > #|kawa:15|# (define test (map/remove-nulls-1 (lambda (x) x) wds-data-str-= split)) > #|kawa:16|# (length test) > 22569 >=20 > i think what prevent the optimisation to work in Kawa was the nesting of = the recursion call in a let, > and the nesting of the tail call in a _cons_ >=20 > Damien=20 >=20 --=20 Damien.Mattei@unice.fr, Damien.Mattei@oca.eu, UNS / OCA / CNRS