Skip to content

Instantly share code, notes, and snippets.

@MichaelBlume
Last active August 29, 2015 14:06
Show Gist options
  • Save MichaelBlume/17a227cc839f52f68c97 to your computer and use it in GitHub Desktop.
Save MichaelBlume/17a227cc839f52f68c97 to your computer and use it in GitHub Desktop.
Find-in
; http://an-animal-imagined-by-poe.tumblr.com/post/97790330928/i-have-been-busy-distracted-absent-for-the-past
(defn find-in
([needle haystack] (find-in needle haystack 'x))
([needle haystack varname]
; varname remminds us of how we got the haystack we're looking at from x
(or
; base case
(when (= needle haystack)
varname)
; if the haystack is a symbol we're screwed
(when-not (symbol? haystack)
(or
; try looking at the first element of the haystack
(when (seq haystack)
(find-in
needle
(first haystack)
(list 'first varname)))
; or in the rest of it?
(when (seq haystack)
(find-in
needle
(rest haystack)
(list 'rest varname)))
; maybe we can cons together the parts of the needle
; from different parts of the haystack
(when-not (symbol? needle)
(when-let
[car (find-in
(first needle)
haystack
varname)]
(when-let
[cdr (find-in
(rest needle)
haystack
varname)]
(list 'cons car cdr)))))))))
(defn exercise-find-in [needle haystack]
(eval
`(let [~'x (quote ~haystack)]
~(find-in needle haystack))))
@ohAitch
Copy link

ohAitch commented Sep 19, 2014

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