Skip to content

Instantly share code, notes, and snippets.

@santanuchakrabarti
Created June 4, 2014 22:09
Show Gist options
  • Save santanuchakrabarti/10e642924c557beba28e to your computer and use it in GitHub Desktop.
Save santanuchakrabarti/10e642924c557beba28e to your computer and use it in GitHub Desktop.
Code I wrote to learn basic Lisp programming
(defun add_positive_numbers (from to)
(/ (* (+ from to) (+ 1 (- to from))) 2))
(add_positive_numbers 20 119)
(defun create_seq (seq_generator seq_items iter_count)
(loop for i from 0 to iter_count
do (let ((nxt_entry (funcall seq_generator seq_items)))
(setf seq_items (append seq_items (list nxt_entry)))))
seq_items)
(defun fibonacci (fib_seq)
(if (or (null fib_seq) (= (length fib_seq) 1))
1
(let ((n (length fib_seq)))
(+ (nth (- n 2) fib_seq) (nth (- n 1) fib_seq)))))
(create_seq #'fibonacci '() 10)
(defun myseq_generator (my_seq)
(if (> (length my_seq) 2)
(let ((n (length my_seq)))
(+ (nth (- n 1) my_seq) (* (nth (- n 3) my_seq) n)))
(second my_seq)))
(create_seq #'myseq_generator '(3 8) 10)
;;Shunting-Yard Algorithm - implementation
;;Algorithm: http://www.geeksforgeeks.org/expression-evaluation/
;;Input: arithmetic expression (as string) in infix notation
;;returns valid Lisp expression (bracketed prefix notation)
(defparameter precedences (pairlis '(#\( #\+ #\- #\* #\/ #\^) '(0 1 1 2 2 2)))
(defun parse-to-prefix (expression)
(if (and (stringp expression) (= (length expression) 0))
nil
(let ((opcode-stack (make-array 0 :fill-pointer t :adjustable t))
(operand-stack (make-array 0 :fill-pointer t :adjustable t)))
(dolist (nxtChar (reverse (coerce expression 'list)))
(case nxtChar
((#\+ #\- #\* #\/ #\^)
;;Manipulate opcode-stack and create partial prefix
(if (> (length opcode-stack) 0)
(progn
(setf prec-curr (cdr (assoc nxtChar precedences)))
(setf opcode-prev (vector-pop opcode-stack))
(setf prec-prev (cdr (assoc opcode-prev precedences)))
(vector-push-extend opcode-prev opcode-stack)
(loop while (>= prec-prev prec-curr)
do
(setf opcode-prev (vector-pop opcode-stack))
(setf operand-1 (vector-pop operand-stack))
(setf operand-2 (vector-pop operand-stack))
(setf operand (concatenate 'string "(" (string opcode-prev) " " operand-2 " " operand-1 ")"))
(vector-push-extend operand operand-stack)
(if (> (length opcode-stack) 0)
(progn
(setf opcode-prev (vector-pop opcode-stack))
(setf prec-prev (cdr (assoc opcode-prev precedences)))
(vector-push-extend opcode-prev opcode-stack))
(return)))
(vector-push-extend nxtChar opcode-stack))
(vector-push-extend nxtChar opcode-stack)))
((#\))
;;Manipulate parenthesis
(let ((nxtOp (vector-pop opcode-stack)))
(loop while (char-not-equal nxtOp #\()
do
(setf operand-1 (vector-pop operand-stack))
(setf operand-2 (vector-pop operand-stack))
(setf operand (concatenate 'string "(" (string nxtOp) " " operand-2 " " operand-1 ")"))
(vector-push-extend operand operand-stack)
(setf nxtOp (vector-pop opcode-stack))
)))
((#\()
;;Push left parenthesis
(vector-push-extend nxtChar opcode-stack))
((#\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\0)
;;Push operands into operand-stack
(vector-push-extend (string nxtChar) operand-stack))
(otherwise nxtChar)))
;;Check if opcode-stack not empty - complete prefix
(loop while (> (length opcode-stack) 0)
do
(setf nxtOp (vector-pop opcode-stack))
(setf operand-1 (vector-pop operand-stack))
(setf operand-2 (vector-pop operand-stack))
(setf operand (concatenate 'string "(" (string nxtOp) " " operand-2 " " operand-1 ")"))
(vector-push-extend operand operand-stack))
(vector-pop operand-stack)
)))
(parse-to-prefix "(5)")
(parse-to-prefix "2+3")
(parse-to-prefix "5-3-1")
(parse-to-prefix "5+3*1")
(parse-to-prefix "5/3-1")
(parse-to-prefix "5/3-1+4")
(parse-to-prefix "5/3-(1+4)")
(parse-to-prefix "(5/3-(1+4))")
(parse-to-prefix "5/(3-2)-(1+4*2)")
(parse-to-prefix "((5/(3-2)-(1+4*2)))")
(parse-to-prefix "(5/(3-2)-(1+4*2))+6/3*(8-5)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment