F-Flat: Quadratic Equation

node v8.17.0
version: 1.0.0
endpointsharetweet
const { Stack } = require('@hypercubed/f-flat@0.0.6'); const f = new Stack(); f.eval(` core: rcl use math: rcl use types: rcl use shuffle: rcl use `).stack;
First we define lambda helpers. `=>` Stores values from the stack into each name in the list. For example `1 2 3 [ x: y: ] =>` assigns 2 to x and 3 to y, leaving 1 on the stack. `lambda-eval` evaluates a lamba in the form of `[ [ args ] => expression ]`. Notice the use of `fork`. Since the vocabulary is write-once, this executes the expression on a child stac, and therefore prevents polluting the local vocab. Finally, `lambda` converts an expression to a lambda expression.
f.eval(` =>: [ [ sto ] [ reverse ] dip foreach ] ; lambda-eval: [ [ stack ] dip unstack: swap >> >> fork unstack ] ; lambda: [ [ lambda-eval ] >> ] ; `).stack;
Where we define the quadratic equation using lambdas (formulation from https://www.reddit.com/r/concatenative/comments/35xpxv/ltu_concatenative_language_kont/cr9ixmb/)
f.eval(` quad: [ [ 'a' 'b' 'c' ] => b -1 * b b * 4 a c * * - sqrt -+ [ 2 a * / ] bi@ ] lambda ; `).stack.toString();
And to test:
f.eval(` \`The roots of 2x^2+5x-3 are $(2 5 -3 quad)\` `).stack.toString();
f.eval(` clr \`The roots of 1x^2+5x-1 are $(1 1 -1 quad)\` `).stack.toString();
f.eval(` clr \`The roots of 2x^2+3x+4 are $(2 3 4 quad)\` `).stack.toString();
Loading…

no comments

    sign in to comment