3 ms·
In a lisp-1: (define inc (lambda (n) (+ n 1))) When referring to `inc` (assuming no shadowing in a different scope), `inc` refers to a function object, we c
by Jtsummers 10y ago
In a lisp-1:
(define inc (lambda (n) (+ n 1)))
When referring to `inc` (assuming no shadowing in a different scope), `inc` refers to a function object, we can do:
(define g (lambda (f) (f 3))
(g inc) ;; yields 4
In a lisp2, it won't work the same way:
* (defun inc (n) (+ 1 n))
* (defun g (f) (f 3))
; in: DEFUN G
; (SB-INT:NAMED-LAMBDA G
; (F)
; (BLOCK G (F 3)))
;
; caught STYLE-WARNING:
; The variable F is defined but never used.
; in: DEFUN G
; (F 3)
;
; caught STYLE-WARNING:
; undefined function: F
;
; compilation unit finished
; Undefined function:
; F
; caught 2 STYLE-WARNING conditions
Instead we can define it like:
* (defun g (f) (apply f '(3))) ;; specifying the parameters as a list
* (g 'inc) ;; get the symbol for inc, as a variable it has no binding
4
* (g inc)
debugger invoked on a UNBOUND-VARIABLE in thread
#<THREAD "main thread" RUNNING {10028F6AF3}>:
The variable INC is unbound.
There are some benefits, though it's been so long since I've used either Scheme or Common Lisp that I can't clearly articulate why, but I recall at the time preferring lisp2 (or lispN, really).
- zeveb 10y agoI'm pretty sure that the lispy way to write the example would be: (defun g (f) (funcall f 3)) (g #'inc) Which I think is actually prettier than the Scheme: (define g (lambda (f) (f 3)) (g inc)
- Jtsummers 10y agoThanks. Couldn't remember funcall but I knew there was a better way.
- lisper 10y agoIt is not hard to make a Lisp2 act like a Lisp1 when you want it to, e.g.: (defmacro define (name&args &body body) (let ((name (car name&args)) (args (cdr name&args))) `(defun ,name ,args (flet ,(mapcar (lambda (arg) `(,arg (&rest args) (apply ,arg args))) args) ,@body)))) ? (DEFINE (F X) (X 1)) F ? (f '1+) 2 It is much (much!) harder to make a Lisp1 act like a Lisp2.