Skip to content

Instantly share code, notes, and snippets.

@ichimal
Created September 18, 2012 12:16
Show Gist options
  • Save ichimal/3742801 to your computer and use it in GitHub Desktop.
Save ichimal/3742801 to your computer and use it in GitHub Desktop.
Z-combinator (varargs version)
(defun z (f)
((lambda (x)
(funcall f (lambda (&rest args)
(apply (funcall x x) args) )))
(lambda (x)
(funcall f (lambda (&rest args)
(apply (funcall x x) args) )))))
(defvar fact
(lambda (f)
(lambda (x)
(if (zerop x) 1 (* x (funcall f (1- x)))) )))
(defvar make-2-tuples
(lambda (f)
(lambda (l1 l2)
(when (and l1 l2)
(cons (cons (car l1) (car l2))
(funcall f (cdr l1) (cdr l2)) )))))
(defvar make-n-tuples
(lambda (f)
(lambda (&rest lists)
(when (every #'identity lists)
(cons (mapcar #'car lists)
(apply f (mapcar #'cdr lists)) )))))
(print (funcall (z fact) 32))
(print (funcall (z make-2-tuples) '(a b c) '(1 2 3)))
(print (funcall (z make-n-tuples) '(a b c) '(1 2 3) '(i j k)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment