Created
March 19, 2012 15:11
-
-
Save gberenfield/2115756 to your computer and use it in GitHub Desktop.
Week 1 - problem #3
This file contains 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
(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