Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Unknown Newsgroups: comp.lang.forth Subject: Re: lisp can sum a list of numbers with 2 parens and a +, can forth useloop to make something similar? Date: Wed, 11 Feb 2015 09:19:16 +0000 (UTC) Organization: A noiseless patient Spider Lines: 55 Message-ID: References: <6c04408e-ec35-4a54-b066-e25ee55f4f07@googlegroups.com> <54d8af54$0$282$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 11 Feb 2015 09:19:16 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="b4e86702a125c9837ba0173f2541fe79"; logging-data="18827"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6OaFDmLv4Dqclb2UjoDdzRf9QsHB2RpQ=" User-Agent: Pan/0.133 (House of Butterflies) Cancel-Lock: sha1:ZFDKBLly/x0suPXQ0kJxvuhlryw= Xref: csiph.com comp.lang.forth:36466 On Mon, 09 Feb 2015 08:00:02 -0500, Doug Hoffman wrote: > On 2/9/15 3:55 AM, WJ wrote: > >> Calculate the average of the numbers in a list, traversing it only once >> and making no assignments to a variable: >> >> Gauche Scheme: >> >> (define (average numbers) >> (apply / >> (fold (lambda (x accum) (map + (list x 1) accum)) >> '(0 0) >> numbers))) >> >> (define numbers `(,@(iota 9 -9 2) 633 ,@(iota 9 22))) numbers >> ===> >> (-9 -7 -5 -3 -1 1 3 5 7 633 22 23 24 25 26 27 28 29 30) >> >> (average numbers) >> ===> >> 858/19 >> >> (inexact (average numbers)) >> ===> >> 45.1578947368421 >> >> >> The easy way would be >> >> (/. (apply + numbers) (length numbers)) >> ===> >> 45.1578947368421 >> >> but that traverses the list twice: once to add up the numbers and once >> to determine the length of the list. > > > : ave{ 0e 0 > begin parse-name 2dup s" }" compare > while >float drop f+ 1+ > repeat 2drop s>f f/ f. ; > > ave{ -9 -7 -5 -3 -1 1 3 5 7 633 22 23 24 25 26 27 28 29 30 } > 45.1578947368421 ok > > \ the easier way would be to use library code: f{ -9 -7 -5 -3 -1 1 3 5 7 > 633 22 23 24 25 26 27 28 29 30 } average: f. 45.1578947368421 ok Computing is no longer 'about computors' it's about human cognition: saving effort. Yes, minimal text is part of saving-effort. But recognise & select [ie. menu] instead of 'coding' is cheaper. Also color economically adds cognitive power. Where's the VISUAL forth?