[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Just for Fun ...



Here's a couple of cute Maclisp toys to play with ...

[1] Fun with Shadowing.

		(DEFMACRO FOO (X) `(CAR ,X))
		(MAPCAR #'FOO '((A B) (C D) (E F)))

      Do you know why it loses?

		(DEFUN FOO (X) (FOO X))
		(DEFMACRO FOO (X) `(CAR ,X))
		(MAPCAR #'FOO (X) '((A B) (C D) (E F)))

      Do you know why it wins?

[2] Fun with Interpreted Macros.

      (DEFMACRO MMAPCAR (FN L)
	(LET ((VAR (GENSYM))
	      (CALL (IF (AND (LISTP FN) (MEMQ (CAR FN) '(QUOTE FUNCTION)))
			`(,(CADR FN))
			`(FUNCALL ,FN))))
	  `((LAMBDA (,VAR)
	     (COND (,VAR (CONS (,@CALL (CAR ,VAR)) (MMAPCAR ,FN (CDR ,VAR))))))
	    ,L)))
      (SETQ GRIND-MACROEXPANDED T)
      (DEFMACRO F (X) `(CAR ,X))
      (MMAPCAR #'F '((A B) (C D) (E F)))
      (SPRINTER +)
      (DEFUN G (X) (MMAPCAR #'F X))
      (G '((A B) (C D)))
      (GRINDEF G)
      (G '((A B)))
      (GRINDEF G)
      (G '((A B) (C D) (E F)))

[3] Fun with Compiled Macros. (Extra Credit)

      Why doesn't the definition of G from [2] compile?