Subdomain Posts
Lisp | 2 days ago
Lisp | 2 days ago
Lisp | 2 days ago
Lisp | 2 days ago
Lisp | 2 days ago
None | 3 days ago
None | 22 days ago
Scheme | 138 days ago
Lisp | 149 days ago
Lisp | 160 days ago
Recent Posts
None | 6 sec ago
None | 12 sec ago
PHP | 16 sec ago
C | 1 min ago
None | 1 min ago
None | 1 min ago
None | 2 min ago
None | 2 min ago
None | 2 min ago
None | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Luis Sergio Oliveira on the 22nd of Oct 2009 06:36:14 AM Download | Raw | Embed | Report
  1. ;;; -*- mode: Lisp; Syntax: COMMON-LISP; coding: utf-8 -*-
  2. ;;; Solution by Luis Sergio Oliveira for the Beautiful Code exercise from
  3. ;;; Programming Praxis
  4. ;;; See http://programmingpraxis.com/2009/09/11/beautiful-code/
  5.  
  6. (defun empty (seq)
  7.   (declare (inline))
  8.   (= 0 (length seq)))
  9.  
  10. (defun match (re text)
  11.   "search for re anywhere in text"
  12.   (if (and (not (empty re)) (char= #\^ (elt re 0)))
  13.       (matchhere (subseq re 1) text)
  14.       ;; must look at empty string
  15.       (cond ((matchhere re text))
  16.             ((empty text)
  17.              nil)
  18.             ((match re (subseq text 1))))))
  19.  
  20. (defun matchhere (re text)
  21.   "search for re at beginning of text"
  22.   (cond ((empty re))
  23.         ((and (< 1 (length re)) (char= #\* (elt re 1)))
  24.          (matchstar (elt re 0) (subseq re 2) text))
  25.         ((and (char= #\$ (elt re 0)) (= 1 (length re)))
  26.          (empty text))
  27.         ((and (not (empty text))
  28.               (or (char= #\. (elt re 0)) (char= (elt re 0) (elt text 0))))
  29.          (matchhere (subseq re 1) (subseq text 1)))))
  30.  
  31. (defun matchstar (c re text)
  32.   "search for c*re at beginning of text"
  33.   (cond ((matchhere re text))
  34.         ((and (not (empty text))
  35.               (or (char= c (elt text 0)) (char= #\. c)))
  36.          (matchstar c re (subseq text 1)))))
  37.  
  38. ;;; Debugging helper:
  39. ;;; (trace matchstar matchhere match)
  40. ;;; (untrace)
  41.  
  42. (defun process-test-result (expected actual test-form)
  43.   (let ((result (equal expected actual)))
  44.     (format t (if result "." "F"))
  45.     (list result (apply #'format nil
  46.                         (if result
  47.                             (list "\"~a\" was successful." test-form)
  48.                             (list "\"~a\" failed, expected ~a but got ~a."
  49.                                   test-form expected actual))))))
  50.  
  51. (defun tests-simple-re-match ()
  52.   (let ((results (list)))
  53.     (macrolet ((test-expression (expected &body test-form)
  54.                  (let ((actual (gensym)))
  55.                    `(let ((,actual ,@test-form))
  56.                       (push (process-test-result ,expected ,actual ',@test-form)
  57.                             results)))))
  58.       (test-expression t (match "ab" "abcd"))
  59.       (test-expression nil (match "ab" "aacd"))
  60.       (test-expression t (match ".a" "aacd"))
  61.       (test-expression t (match "a*c" "aacd"))
  62.       (test-expression t (match "a*c" "cd"))
  63.       (test-expression t (match "a*d" "cd"))
  64.       (test-expression nil (match "a*dd" "cd"))
  65.       (test-expression t (match "ola.*" "ola luis"))
  66.       (test-expression t (match "^ola..ui*s$" "ola luis"))
  67.       (test-expression t (match ".ui*s$" "ola luis"))
  68.       (test-expression t (match "a*" ""))
  69.       (test-expression nil (match "a" ""))
  70.       (test-expression nil (match "." ""))
  71.       (test-expression t (match "" "ola")) ; should this return NIL?
  72.       (test-expression t (match "^" "ola")); how about this?
  73.       ;; test suite from Programming Praxis solution
  74.       ;; http://programmingpraxis.codepad.org/vv72J3XE
  75.       (test-expression t (match "a" "a"))
  76.       (test-expression nil (match "a" "b"))
  77.       (test-expression t (match "a" "ab"))
  78.       (test-expression t (match "a" "ba"))
  79.       (test-expression t (match "ab" "ab"))
  80.       (test-expression nil (match "ab" "ba"))
  81.       (test-expression t (match "ab" "xab"))
  82.       (test-expression t (match "ab" "aab"))
  83.       (test-expression nil (match "a.c" "ac"))
  84.       (test-expression t (match "a.c" "abc"))
  85.       (test-expression nil (match "a.c" "xac"))
  86.       (test-expression t (match "a.c" "xabcx"))
  87.       (test-expression t (match "^ab" "ab"))
  88.       (test-expression nil (match "^ab" "ba"))
  89.       (test-expression nil (match "^ab" "aab"))
  90.       (test-expression t (match "^ab" "abc"))
  91.       (test-expression t (match "ab$" "ab"))
  92.       (test-expression nil (match "ab$" "ba"))
  93.       (test-expression t (match "ab$" "aab"))
  94.       (test-expression nil (match "ab$" "abc"))
  95.       (test-expression t (match "^ab$" "ab"))
  96.       (test-expression nil (match "^ab$" "ba"))
  97.       (test-expression nil (match "^ab$" "abc"))
  98.       (test-expression nil (match "^ab$" "aba"))
  99.       (test-expression t (match "a.*c" "ac"))
  100.       (test-expression t (match "a.*c" "abc"))
  101.       (test-expression t (match "a.*c" "abbc"))
  102.       (test-expression nil (match "a.*c" "cbba"))
  103.       (test-expression nil (match "aa*" "x"))
  104.       (test-expression t (match "aa*" "a"))
  105.       (test-expression t (match "aa*" "aa"))
  106.       (test-expression t (match "aa*" "ba"))
  107.       (test-expression t (match "a*a*a" "a"))
  108.       (test-expression t (match "a*a*a" "aaa"))
  109.       (test-expression nil (match "a*a*a" "xxxxx"))
  110.       (dolist (result results)
  111.         (unless (first result)
  112.           (format t "~&~a" (second result)))))))
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: