fork download
  1. (ns wildcards
  2. (:refer-clojure))
  3.  
  4. (defn- dpr
  5. "Debug printing with poor man's indendation"
  6. [pattern & rest]
  7. (print (repeat (- 6 (count pattern)) " "))
  8. (apply println rest))
  9.  
  10. (defn wildcard-match [input pattern]
  11. (println "wildcard-match " input pattern)
  12. (if (or (empty? input) (empty? pattern))
  13. ;; One is empty, return true if both are
  14. (and (empty? input) (empty? pattern))
  15. ;; Else
  16. (if (= (first pattern) \?)
  17. ;; Wildcard, so with short ciruiting or:
  18. (or (do
  19. (dpr pattern "Try to match no character...")
  20. (wildcard-match input (rest pattern)))
  21. (do
  22. (dpr pattern "Ok, so try to match any character...")
  23. (recur (rest input) (rest pattern))))
  24. ;; Non-Wildcard, test for equality, and if equal, go on.
  25. (and (= (first pattern) (first input))
  26. (recur (rest input) (rest pattern))))))
  27.  
  28.  
  29.  
  30. (defn testcase [input pattern]
  31. (println "#####################################")
  32. (println "Trying to match" input "with" pattern)
  33. (println "=>" (wildcard-match (seq input) (seq pattern)))
  34. (println))
  35.  
  36.  
  37. (doall (map #(testcase (first %) (second %))
  38. [["hello" "hello"]
  39. ["hello" "h?l?o"]
  40. ["hllo" "h?l?o"]
  41. ["hlo" "h??lo"]
  42. ["hello" "h?lo"]
  43. ["hello" "h???p"]]))
  44.  
Success #stdin #stdout 2.06s 335488KB
stdin
Standard input is empty
stdout
#####################################
Trying to match hello with hello
wildcard-match  (h e l l o) (h e l l o)
wildcard-match  (e l l o) (e l l o)
wildcard-match  (l l o) (l l o)
wildcard-match  (l o) (l o)
wildcard-match  (o) (o)
wildcard-match  () ()
=> true

#####################################
Trying to match hello with h?l?o
wildcard-match  (h e l l o) (h ? l ? o)
wildcard-match  (e l l o) (? l ? o)
(   )Try to match no character...
wildcard-match  (e l l o) (l ? o)
(   )Ok, so try to match any character...
wildcard-match  (l l o) (l ? o)
wildcard-match  (l o) (? o)
(       )Try to match no character...
wildcard-match  (l o) (o)
(       )Ok, so try to match any character...
wildcard-match  (o) (o)
wildcard-match  () ()
=> true

#####################################
Trying to match hllo with h?l?o
wildcard-match  (h l l o) (h ? l ? o)
wildcard-match  (l l o) (? l ? o)
(   )Try to match no character...
wildcard-match  (l l o) (l ? o)
wildcard-match  (l o) (? o)
(       )Try to match no character...
wildcard-match  (l o) (o)
(       )Ok, so try to match any character...
wildcard-match  (o) (o)
wildcard-match  () ()
=> true

#####################################
Trying to match hlo with h??lo
wildcard-match  (h l o) (h ? ? l o)
wildcard-match  (l o) (? ? l o)
(   )Try to match no character...
wildcard-match  (l o) (? l o)
(     )Try to match no character...
wildcard-match  (l o) (l o)
wildcard-match  (o) (o)
wildcard-match  () ()
=> true

#####################################
Trying to match hello with h?lo
wildcard-match  (h e l l o) (h ? l o)
wildcard-match  (e l l o) (? l o)
(     )Try to match no character...
wildcard-match  (e l l o) (l o)
(     )Ok, so try to match any character...
wildcard-match  (l l o) (l o)
wildcard-match  (l o) (o)
=> false

#####################################
Trying to match hello with h???p
wildcard-match  (h e l l o) (h ? ? ? p)
wildcard-match  (e l l o) (? ? ? p)
(   )Try to match no character...
wildcard-match  (e l l o) (? ? p)
(     )Try to match no character...
wildcard-match  (e l l o) (? p)
(       )Try to match no character...
wildcard-match  (e l l o) (p)
(       )Ok, so try to match any character...
wildcard-match  (l l o) (p)
(     )Ok, so try to match any character...
wildcard-match  (l l o) (? p)
(       )Try to match no character...
wildcard-match  (l l o) (p)
(       )Ok, so try to match any character...
wildcard-match  (l o) (p)
(   )Ok, so try to match any character...
wildcard-match  (l l o) (? ? p)
(     )Try to match no character...
wildcard-match  (l l o) (? p)
(       )Try to match no character...
wildcard-match  (l l o) (p)
(       )Ok, so try to match any character...
wildcard-match  (l o) (p)
(     )Ok, so try to match any character...
wildcard-match  (l o) (? p)
(       )Try to match no character...
wildcard-match  (l o) (p)
(       )Ok, so try to match any character...
wildcard-match  (o) (p)
=> false