Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Programming Thread

Name: Anonymous 2013-10-03 0:11

This thread is about /prog/ramming for /prog/rammers and /DPT/ers

What are you working on?

Name: Anonymous 2013-10-03 0:30

>>12
That's exactly what I do, actually. I just pick the numbers on the stack and run the command on them, then put the result of that function call onto the stack.
Of course, it assumes you use numbers so things like string manipulation aren't possible (to be fair, putting something like that in a calculator would be bloat).

For example, this is a big list of functions that I put in the code.

(define functions
  (list
    (list "pi"    3.14159265359        0)
    (list "e"     2.71828182845        0)
    (list "id"    identity             1)
    (list "add1"  add1                 1)
    (list "sub1"  sub1                 1)
    (list "abs"   abs                  1)
    (list "cos"   cos                  1)
    (list "sin"   sin                  1)
    (list "tan"   tan                  1)
    (list "nl"    log                  1)
    (list "phi"   phi                  1)
    (list "sqrt"  sqrt                 1)
    (list "round" int-round            1)
    (list "+"     +                    2)
    (list "*"     *                    2)
    (list "/"     /                    2)
    (list "-"     -                    2)
    (list "**"    expt                 2)
    (list "ack"   ack                  2)
    (list "mod"   modulo               2)
    (list "gcd"   gcd                  2)
    (list "log"   log-b                2)))


The first member of every list is the string which is matched to the command run by the user. The second item of the list is either a value or a symbol which is assigned to a function (incidentally, you can use lambda expressions here, so add1 could be (lambda (x) (+ 1 x)) instead).

Adding a member to the list is the same as adding a new function, so if I wanted an add3 command, I could just add

(list "add3" (lambda (x) (+ 3 x) 1)

to the list and it'll work.

The third argument is the number of arguments needed for the function. So you can't do:
10 10 10 +
and expect to get thirty. A "function" with 0 arguments just puts the number onto the stack (useful for things like pi or e).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List