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


Groups > comp.lang.forth > #34873

Re: Josephus Circle problem

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Josephus Circle problem
Date 2014-12-11 17:57 +0000
Organization A noiseless patient Spider
Message-ID <m6clud$pc8$1@dont-email.me> (permalink)
References (2 earlier) <ccb911cb-e3e5-46e1-9bdb-4da9d16aef50@googlegroups.com> <383caaa9-5308-4d58-b118-eec96803840e@googlegroups.com> <l9h1q6$glb$1@dont-email.me> <slrnlbofou.6u6.st@tumtum.plumbweb.net> <7xvbybbq9l.fsf@ruckus.brouhaha.com>

Show all headers | View raw


Paul Rubin wrote:

> Tristan Plumb <firth@trstn.net> writes:
> > However! Combinators can do amazing things with quotations. Take nrec:...
> > : nrec ( n F F0 -- Fn ) rot 1+ 1 do over i swap execute loop nip ;
> 
> Neat ;-)
> 
> > : (josephus) swap 2 + swap mod 1+ ;
> > : josephus ' (josephus) 1 nrec ;
> 
> ' should say ['] .
> 
> The Haskell equivalent of Marcel's algorithm is:
> 
>   josephus k n = 1 + foldl (\a b->(a+k)`mod`b) 0 [1..n]
> 
> I guess it's possible to concoct a reduce combinator in Forth, but it
> just doesn't seem in the Forth style.  It would also want to use a
> coroutine to generate the input stream.

In OCaml, using recursion:

let rec jo a = function
    41 -> 1+a
  |  b -> jo ((a+3) mod b) (b+1) ;;

jo 0 1 ;;

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

Re: Josephus Circle problem "WJ" <w_a_x_man@yahoo.com> - 2014-12-11 17:57 +0000

csiph-web