Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #34846 > unrolled thread

Re: Jensen's device (was: Rosetta Code: ...)

Started by"WJ" <w_a_x_man@yahoo.com>
First post2014-12-09 00:05 +0000
Last post2014-12-10 15:25 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.forth

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Jensen's device (was: Rosetta Code: ...) "WJ" <w_a_x_man@yahoo.com> - 2014-12-09 00:05 +0000
    Re: Jensen's device (was: Rosetta Code: ...) albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-12-10 15:25 +0000

#34846 — Re: Jensen's device (was: Rosetta Code: ...)

From"WJ" <w_a_x_man@yahoo.com>
Date2014-12-09 00:05 +0000
SubjectRe: Jensen's device (was: Rosetta Code: ...)
Message-ID<m65ec8$lsv$1@dont-email.me>
Anton Ertl wrote:

> Hans Bezemer <the.beez.speaks@gmail.com> writes:
> > Take a look at http://rosettacode.org/wiki/Jensen%27s_Device#Forth
> > 
> > I did it in two lines.
> 
> These lines are:
> 
> : sum 0 s>f 1+ swap ?do i over execute f+ loop drop ;
> :noname s>f 1 s>f fswap f/ ; 1 100 sum f.
> 
> Whereas the core of the original is:
> 
> print (sum (i, 1, 100, 1/i))
> 
> where the parameters are passed by name.
> 
> However, you pass i to the :noname definition on the stack, not by
> name.  The following passes both i and 1/i as an xt:
> 
> fvariable ii \ I is a Forth word that we need
> : sum ( xt1 lo hi xt2 -- r )
>   0e swap 1+ rot ?do ( addr xt r1 )
>     i s>f over execute f! dup execute f+
>   loop 2drop ;
> ' ii 1 100 :noname 1e ii f@ f/ ; sum f.
> 

Gauche Scheme:

(use gauche.parameter)

(let1 i (make-parameter 0.0)
  (define (sum  i lo hi term)
    (let1 tmp 0.0
      (i lo)
      (while (<= (i) hi)
        (inc! tmp (term))
        (i (+ 1 (i)))) 
      tmp))
  (sum i 1 100 (cut / (i))))

Another way:

(let1 i (make-parameter 0.0)
  (define (sum  i lo hi term)
    (let1 tmp 0.0
      (i lo)
      (parameter-observer-add! i (^(a b) (inc! tmp (term))) 'before)
      (while (<= (i) hi) (i (+ 1 (i)))) 
      tmp))
  (sum i 1 100 (cut / (i))))

[toc] | [next] | [standalone]


#34861

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2014-12-10 15:25 +0000
Message-ID<548865e3$0$6881$e4fe514c@dreader36.news.xs4all.nl>
In reply to#34846
In article <m65ec8$lsv$1@dont-email.me>, WJ <w_a_x_man@yahoo.com> wrote:
>Anton Ertl wrote:
>
>> Hans Bezemer <the.beez.speaks@gmail.com> writes:
>> > Take a look at http://rosettacode.org/wiki/Jensen%27s_Device#Forth
>> >
>> > I did it in two lines.
>>
>> These lines are:
>>
>> : sum 0 s>f 1+ swap ?do i over execute f+ loop drop ;
>> :noname s>f 1 s>f fswap f/ ; 1 100 sum f.
>>
>> Whereas the core of the original is:
>>
>> print (sum (i, 1, 100, 1/i))
>>
>> where the parameters are passed by name.
>>
>> However, you pass i to the :noname definition on the stack, not by
>> name.  The following passes both i and 1/i as an xt:
>>
>> fvariable ii \ I is a Forth word that we need
>> : sum ( xt1 lo hi xt2 -- r )
>>   0e swap 1+ rot ?do ( addr xt r1 )
>>     i s>f over execute f! dup execute f+
>>   loop 2drop ;
>> ' ii 1 100 :noname 1e ii f@ f/ ; sum f.
>>

Sorry to respond to a response. I missed the original message.

Jensen device is to be banned. It was a side effect of how
parameters are passed in Algol 60.
What you want to do in fact is calculate
print (sum( range=[1,100], lambda=(x --> 1/x) ) )
That is the correct formulation of the question.
A language that even allows Jensens device is poorly designed,
and in fact as such it no longer worked in Algol 68 where
1. all parameters are passed by value
   (note that a lambda function is a value!)
2. parameters can be calculated concurrently, and if you
 want to calculate them in sequence you have to change
 the call to
   print (sum (i; 1; 100; 1/i))
which gives it away.

There is no way that inside sum, changing i could affect some
other parameter passed to cum().

So I think the Rosetta question should be reformulated, and
I also think that Hans' solution is correct inasfar possible.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web