Thursday, August 22, 2013

Problem 9 redone in Forth

Another example of redoing a problem for which I used up a known language too early...this time I recovered Javascript by using Forth, which is a pretty old stack-based language. All the documentation I could find online was old and had everything in all-caps...so that apparently influenced my code:
: NUMS BEGIN DUP 1 - DUP 1 =  UNTIL ;
: NEWTON { t x } t x t / + 2 / DUP t ;
: SQRT { x } x 2 / BEGIN x NEWTON SWAP - 2 < UNTIL ;
: ISSQUARE { x } x SQRT DUP * x = ;
: CSQUARED { a b } a a * b b * + ;
: TEST { a b } a b CSQUARED ISSQUARE IF a b a b CSQUARED SQRT 
  + + 1000 = IF a b a b CSQUARED SQRT * * . CR bye THEN THEN ;
: INNERLOOP { a } 800 NUMS BEGIN DUP a TEST 800 = UNTIL ;
: OUTERLOOP 800 NUMS BEGIN INNERLOOP DUP 800 = UNTIL ;  
OUTERLOOP

2 comments:

  1. This is James' former favorite language, by the way. If you want to hear more about it I suggest you ask him.

    ReplyDelete
  2. Oh, I did not know that - my experience with the language was made slightly more frustrating by the fact that I wrote this solution in a car today without access to documentation, so I couldn't look up some important stuff, but I think it would be fair to say that this is the most user-friendly stack-based language I have used yet.

    In other news, I am about to start looking at writing a Shakespeare compiler...

    ReplyDelete