Skip to content

Instantly share code, notes, and snippets.

@gilesbowkett
Created June 30, 2013 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbowkett/5893860 to your computer and use it in GitHub Desktop.
Save gilesbowkett/5893860 to your computer and use it in GitHub Desktop.
comparing every element in a list to every other element
; common lisp version, totally works
; (defun build (list1 list2)
; (if (null list1)
; ()
; (append (mapcar #'(lambda (bubble)
; (list (first list1) bubble))
; list2)
; (build (rest list1) list2))))
; if you tell lisp: (build '(1 2) '(1 2))
; lisp will happily reply: ((1 1) (1 2) (2 1) (2 2))
; literal clojure translation
(defn build [list-1 list-2]
(if (nil? list-1)
()
(concat (map (fn [bubble]
(list (first list-1) bubble))
list-2)
(build (rest list-1) list-2))))
; if you tell clojure: (build '(1 2) '(1 2))
; lisp will stack overflow error all up in your business
; if you're curious there's a Ruby version here:
; https://github.com/gilesbowkett/tweenr/blob/master/bit101_style.rb#L2
@lispm
Copy link

lispm commented Jan 8, 2015

The Common Lisp code reimplements MAPCAN. Use it:

(defun cp (l1 l2)
  (mapcan (lambda (e2)
            (mapcar (lambda (e1) (list e1 e2)) l1))
          l2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment