4 ms·
A while ago I stumbled upon a stack-based language called V: http://code.google.com/p/v-language/ http://code.google.com/p/v-language/ On the very first page i
by zephyrfalcon 16y ago
A while ago I stumbled upon a stack-based language called V: http://code.google.com/p/v-language/ http://code.google.com/p/v-language/
On the very first page is V's version of the quadratic equation problem:
[quad-formula
[a b c] let
[minisub 0 b -].
[radical b b * 4 a * c * - sqrt].
[divisor 2 a *].
[root1 minisub radical + divisor /].
[root2 minisub radical - divisor /].
root1 root2
].
|2 4 -30 quad-formula ??
=(-5.0 3.0)
From what I understand, V encourages the use of local variables; it is also more of a high-level language, like Joy, as opposed to being close to the "bare metal" like Forth.
- kragen 16y agoThis may be lower-level, with all the explicit type conversions, but it doesn't look much more complicated to me. Maybe I'm fooling myself? variable a variable b variable c fvariable discriminant : b^2 b @ dup * ; : 4ac 4 a @ c @ * * ; : s>f s>d d>f ; : -b b @ negate s>f ; : /2a a @ 2* s>f f/ ; : f. f>d d. ; : quadeq c ! b ! a ! b^2 4ac - s>f fsqrt discriminant f! -b discriminant f@ f+ /2a -b discriminant f@ f- /2a ; Usage: : kragen@inexorable:~/devel/inexorable-misc ; gforth quadeq.fs Gforth 0.6.2, Copyright (C) 1995-2003 Free Software Foundation, Inc. Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license' Type `bye' to exit 2 4 -30 quadeq f. f. -5. 3. ok I'd dispense with the floating-point nonsense altogether, but ANS Forth, like C, only defines a square-root routine in floating-point. It's a little more code to define in Forth than in C: variable n : improve n @ over / + 2/ ; : far? - abs 1 > ; : sqrt dup n ! dup if begin dup improve swap over far? while repeat then dup dup * n @ > if 1- then ; But that hardly seems like a strong recommendation. Edit: someone else's cute Newton's-method square-root is at http://jasonwoof.org/sqrt.fs http://jasonwoof.org/sqrt.fs --- it looks quite a bit simpler!
- kragen 16y agoOn further inspection, the jasonwoof code is quite a bit simpler not only because it has a better approach (which it does!) but also because it leaves out the checks for 0 and off-by-one. So it crashes on 0 and gives the wrong answer for 6, saying 3 when the answer correct to two places is 2.45. You can correct the wrong answers with the same dup dup * n @ > if 1- then business that I was using, assuming you want floor(sqrt(n)) instead of, say, round(sqrt(n)) or ceil(sqrt(n)).