Last active
August 29, 2015 14:06
-
-
Save MichaelBlume/17a227cc839f52f68c97 to your computer and use it in GitHub Desktop.
Find-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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)))) |
Slightly more developed interactive demo: http://sondel-forsut.urbit.org/home/pub/match?hay=._am_sum_des_kol__&man=._am_.~-am~-sum~-~-__
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In rosetta-code interests, slightly unconventional to be more verbatim: