Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created November 3, 2012 17:45
Show Gist options
  • Select an option

  • Save tlehman/4008096 to your computer and use it in GitHub Desktop.

Select an option

Save tlehman/4008096 to your computer and use it in GitHub Desktop.
(and robot baby)
;; (quoted-from (short (story (title (and robot baby)))))
(setq line "(Implies (Non-literal-interpretation x) y) (Value (Obey x) (* 0.5 (Value (Obey y)))))")
"(Implies (Non-literal-interpretation x) y) (Value (Obey x) (* 0.5 (Value (Obey y)))))"
;; check if expr (list of chars of some expression) is balanced
(defun balanced (expr)
(defvar lparen (string-to-char "("))
(defvar rparen (string-to-char ")"))
(defun is-open (c) (eq lparen c))
(defun is-close (c) (eq rparen c))
(defun is-empty (ls) (eq ls nil))
(defun is-matching (l r) (and (is-open l) (is-close r)))
(defun is-balanced (list stack)
(cond ((is-empty list) (is-empty stack))
((is-open (car list))
(is-balanced (cdr list) ;; 'push' open-paren onto stack
(cons (car list) stack)))
((is-close (car list))
(if (is-empty stack) nil
(and
(is-balanced (cdr list) (cdr stack))
(is-matching (car stack) (car list)))))
(t (is-balanced (cdr list) stack))))
(is-balanced (string-to-list expr) nil))
balanced
(balanced "(balanced () (() )")
nil
(balanced line)
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment