> 1 <

Автор Сообщение

Яков Замир Кацман (нью)

Members


Статус

15 сообщений

Где: Russia
Род занятий:
Возраст:

#7796   2017-11-06 13:25 GMT+3 часа(ов)      
;;  *All what you need know about macros in lisp*
 
;;Like a function, a macro consists of a name, a parameter list, an optional
;;documentation string, and a body of Lisp expressions.However, as I just
;;discussed, the job of a macro isn't to do anything directly­­its job is to
;;generate code that will later do what you want.
;; [1] http://www.gigamonkeys.com/book/a-few-format-recipes.html
;; [2] https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node51.html
;; (L) J.Z.Katsman (2017) list.development@gmail.com | jzkatsman @ jabber.ru
 
;; define macros
(defmacro when-condition (condition &rest body)
`(if ,condition (progn ,@body)
(format t "~% a is more than ~a" (nth 2 '(,@condition))))
)
 
;; assingment variable
(setq a 220)
220
;; call macro statement
(when-condition (< a 10) (princ "little number"))
 
;; call macro expand routine
(macroexpand-1 '(when-condition (< a 10) (princ "little number")))
(IF (< A 10) (PROGN (PRINC "little number")) (FORMAT T "~% a is more than 10"))
 
;; Macros build construction explaining. Сonsist from 2 elements
;; First !
CL-USER 5 > (setq b '(1 2 3))
(1 2 3)
 
CL-USER 6 > `(a ,@b c)
(A 1 2 3 C)
 
;; Second!
;; Commas work no matter how deeply they appear
;; within a nested list:
CL-USER 7 > (setf a 1 b 2 c 3)
3
 
CL-USER 8 > `(a ,b c)
(A 2 C)
 
CL-USER 9 > `(a (,b c))
(A (2 C))
 
;; Yours sincerely, J.Z.Katsman / Enjoy folks!

отредактировал(а) Яков Замир Кацман (нью): 2017-11-06 15:06 GMT+3 часа(ов)
> 1 <


Онлайн :

0 пользователь(ей), 25 гость(ей) :




Реклама на сайте: