Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Created March 18, 2012 04:12
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 mikebridge/2068786 to your computer and use it in GitHub Desktop.
Save mikebridge/2068786 to your computer and use it in GitHub Desktop.
Codelesson Clojure Assignment 3
(ns codelesson.assignment-3)
(defn match-element
[pattern-el data-el]
{:pre [(not (coll? pattern-el)) ]
:post [(or (true? %) (false? %))] }
(or
(= pattern-el data-el)
(= '? pattern-el)))
(defn match-list
[pattern-list data-list]
{:post [(or (false? %) (true? %))] }
(if (not (coll? pattern-list))
(match-element pattern-list data-list)
(or (and
(empty? pattern-list)
(empty? data-list)) ; both leaf-nodes are empty, halt
(and
(match-list (first pattern-list) (first data-list))
(match-list (rest pattern-list) (rest data-list))))))
(defn match?
"Determine if a string representation of a sexp matches a string representation of data, according to the description of assignment 2"
[pattern data]
{:pre [(string? pattern) (string? data)]
:post [(or (true? %) (false? %))] }
(let [pattern-coll (read-string pattern)
data-coll (read-string data)]
(match-list pattern-coll data-coll)))
@mikebridge
Copy link
Author

(ns codelesson.test.assignment-3
  (:use codelesson.assignment-3)
  (:use clojure.test))


(deftest test-1
  (is (match?
              "(EQU (COLOR TABLE) ?)"
              "(EQU (COLOR TABLE) RED)")))

(deftest test-2
  (is (match?
              "(EQU (COLOR TABLE) ?) "
              "(EQU (COLOR TABLE) (COLOR CHAIR))")))

(deftest test-3
  (is (match?
              "(EQU (COLOR ?) ?)"
              "(EQU (COLOR TABLE) RED)")))

(deftest test-4
  (is (not (match?
              "(EQU (COLOR TABLE) ?)"
              "(EQU (COLOR CHAIR) RED)"))))

(deftest test-5
  (is (not (match?
              "(EQU (COLOR TABLE) ?)"
              "(EQU (COLOR TABLE) RED BROWN)"))))

(deftest test-vectors
  (is (match?
              "[EQU [COLOR ?] ?]"
              "[EQU [COLOR TABLE] RED]")))

(deftest match-string-element
  (is (match-element "test" "test")))

(deftest unmatch-string-element
  (is (not (match-element "x" "y"))))

(deftest match-pattern-element
  (is (match-element '? "test")))

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