wolfgangj / bone-lisp

The Bone Lisp programming language
Other
321 stars 21 forks source link

What's the precise definition of the shortcut for subs? #18

Closed ownwaterloo closed 8 years ago

ownwaterloo commented 8 years ago

Does it begin with | and end with the first list? All the symbols in the middle are parameters? In this case, the following subroutines: (lambda () (foo) (bar)) ; multiple expressions in body (lambda (x) (lambda (y) x)) ; symbol as body in the inner lambda expression (lambda xs (foo)) ; rest arguments couldn't be written as shortcut?

wolfgangj commented 8 years ago

Basically, yes. You could write the first one as | (do (foo) (bar)), though. But personally, I would use lambda in this case. The short notation is intended for short subroutines. It's similar to [+ _ 1] in Arc, but slightly more flexible. It is not intended to replace lambda. But it is very convenient on the REPL.

The second of your examples is not possible (neither is it common).

Rest arguments actually are possible: | . xs (foo).

wolfgangj commented 8 years ago

I think the biggest limitation is in combination with quasiquote, e.g. | ,a (foo) will not work as expected, because ,a is a list and will be considered to be the body by the reader. But this can be considered a bug and I want to fix this eventually.

ownwaterloo commented 8 years ago

First of all, I'm not criticizing it. I'm just curious about it. Many programming languages have some shortcut for subs and I want to know the capability and limitation of them.

Yes, the first one could be written by putting expressions in do. So the inner lambda expression in the second one could also be written as | y (do x), right?

I'm confused about the rest arguments: | . xs (foo). The shortcut notation is handled by the reader and the text "| . xs (foo)" will be read as (lambda xs (foo)), is that true?

I didn't think about quasiquote before. Thanks for pointing it out.

wolfgangj commented 8 years ago

Okay, but you're also welcome to criticize it if you want. :)

You're right, | y (do x) could be used.

Since argument lists in a lambda can have rest arguments like in Scheme (i.e. (a b . r)), I added this for the short form as well: | a b . r (...). I don't think that it is particularly beautiful, but if someone expects this to work they shouldn't be disappointed (and it was easy to do). The side-effect is that | . r (...) also works and is indeed equal to (lambda r (...)).

ownwaterloo commented 8 years ago

Understood. Thanks for you explain. :)