Skip to content

Instantly share code, notes, and snippets.

@gberenfield
Created March 19, 2012 15:11
Show Gist options
  • Save gberenfield/2115756 to your computer and use it in GitHub Desktop.
Save gberenfield/2115756 to your computer and use it in GitHub Desktop.
Week 1 - problem #3
(ns codelesson.week1-3)
(defn debugme [cp cd rp rd]
(prn cp cd )
(prn rp)
(prn rd)
(println "-------------"))
(defn nomore [a b]
(and (empty? a) (empty? b)))
(defn match? [pattern data]
(let [cp (first pattern) cd (first data) rest-p (rest pattern) rest-d (rest data)]
;(debugme cp cd rest-p rest-d)
(cond
(and (or (= cp '?) (= cp cd)) (nomore rest-p rest-d)) true
(seq? cp) (and (match? cp cd) (match? rest-p rest-d))
(not= cp cd) false
:default (match? rest-p rest-d))))
(match? '(?) '(abacab))
(match? '(TEST) '(TEST))
(match? '(EQU (COLOR TABLE) ?) '(EQU (COLOR TABLE) GREEN))
(match? '(EQU (COLOR TABLE) ?) '(EQU (COLOR TABLE) (COLOR GREEN)))
(match? '(EQU (COLOR ?) ?) '(EQU (COLOR CHAIR) ASD))
; falsies
(match? '(TEST) '(TESDT))
(match? '(EQU (COLOR CABINET) ?) '(EQU (COLOR TABLE) GREEN))
(match? '(EQU (COLOR TABLE) ?) '(EQU (COLOR TABLE) RED GREEN))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment