h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
310 stars 23 forks source link

I am cannot applying #112

Closed ghost closed 3 years ago

ghost commented 3 years ago
((1 2 3) (dup *) map) =>

or

((1 2 3) (dup *) map) apply

Result are:

(!) <repl>(1,20) [map]: Incorrect values found on the stack:
- expected: quot quot map
- got:      int map
    <repl>(1,20) in symbol: map
    <repl>(1,27) in symbol: apply
h3rald commented 3 years ago

You don't need apply... map is enough for what you are doing:

 ; pushes (1 4 9) on the stack
 (1 2 3) (dup *) map
ghost commented 3 years ago

You don't need apply... map is enough for what you are doing:

 ; pushes (1 4 9) on the stack
 (1 2 3) (dup *) map

I know i don't need apply but um... why isn't apply working?

(1 2 +) apply ; -> (3)
(4 3 -) apply ; -> (1)
((1 2 3) (dup *) map) apply ; -> error
((1 2 3) (dup *) map) => ; -> error
h3rald commented 3 years ago

Well... apply works by evaluating each item in the quotation in a separate stack.

So here's what happens:

It's like writing 1 2 9 map, which of course doesn't make much sense.

apply is typically useful to evaluate symbols within a quotation:

1 :a
2 :b
3 :c

(a b c) apply ;outputs (1 2 3)

If you simply want to dequote the outer quotation, use dequote or ->: quotation:

((1 2 3) (dup *) map) ->
; outputs: (1 4 9)
ghost commented 3 years ago

Well... apply works by evaluating each item in the quotation in a separate stack.

So here's what happens:

  • a new stack is created
  • (1 2 3) is evaluated, so 1, 2, and 3 are pushed on the stack:

    • stack: 1
    • stack: 1 2
    • stack: 1 2 3
  • (dup *) is evaluated so dup and * are pushed on the stack:

    • stack: 1 2 3 3
    • stack: 1 2 9
  • finally, map is pushed on the stack:

    • stack:
      
      (!) <repl>(1,20) [map]: Incorrect values found on the stack:
    • expected: quot quot map
    • got: int map

It's like writing 1 2 9 map, which of course doesn't make much sense.

apply is typically useful to evaluate symbols within a quotation:

1 :a
2 :b
3 :c

(a b c) apply ;outputs (1 2 3)

If you simply want to dequote the outer quotation, use dequote or ->: quotation:

((1 2 3) (dup *) map) ->
; outputs: (1 4 9)

I was playing with the builtin symbols of min v0.3.0 and then i found your (1 2 3 4 5) (dup *) map example. I decided to try it within quotes. After that, i got this error that i presented it to you for solving it. Exactly, symbols are very confusy things.

But thanks so much!