Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax and variables in Scheme

Tags:

scheme

obviously, this code should fail : (define or 5)

but I was wondering why does this code fail to run :

(define apply 
    (lambda (x y f)
        (f x y)
    )
)
(apply #f #t or)

(or #f #t), would work as expected.

I'm not defining any new variable with a saved name, only passing the function or as an argument.

and (apply 1 2 +) on the other hand works...

like image 733
Sportalcraft Avatar asked Mar 13 '26 22:03

Sportalcraft


1 Answers

or is a special form. It isn't a function. So it can not be passed like that as an argument. Rather than (apply #f #t or), you must use:

(apply #f #t (lambda (a b) (or a b)))

(define or 5) does not fail. It shadows the or special form. Some implementations may not allow redefinition either within a module or of given symbols. So when asking about Scheme it is important to specific the implementation.

This is because special forms can only occur in the first position. Special forms are implemented as macro expansions. For example: (or a b) => (let ((v a)) (if v v b))

like image 103
Dan D. Avatar answered Mar 17 '26 02:03

Dan D.