From 1e690546f80d46bb908c3c4feb362bb085783019 Mon Sep 17 00:00:00 2001 From: David Vazquez Date: Mon, 17 Dec 2012 18:47:10 +0000 Subject: [PATCH] NIL, T, TRUNCATE --- test.lisp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/test.lisp b/test.lisp index 0529823..9871a00 100644 --- a/test.lisp +++ b/test.lisp @@ -12,6 +12,9 @@ (%compile-defvar ',name)) (setq ,name ,value))) +(defvar nil 'nil) +(defvar t 't) + (defmacro defun (name args &rest body) `(progn (eval-when-compile @@ -25,9 +28,18 @@ (defun / (x y) (/ x y)) (defun 1+ (x) (+ x 1)) (defun 1- (x) (- x 1)) +(defun zerop (x) (= x 0)) +(defun not (x) (if x nil t)) + +(defun truncate (x y) (floor (/ x y))) + (defun cons (x y ) (cons x y)) (defun car (x) (car x)) +(defun caar (x) (car (car x))) +(defun cadr (x) (car (cdr x))) (defun cdr (x) (cdr x)) +(defun cdar (x) (cdr (car x))) +(defun cddr (x) (cdr (cdr x))) (defun append (list1 list2) (if (null list1) @@ -62,3 +74,60 @@ (defun find-symbol (name) (get *package* name)) + + +(defmacro cond (&rest clausules) + (if (null clausules) + nil + (if (eq (caar clausules) t) + `(progn ,@(cdar clausules)) + `(if ,(caar clausules) + (progn ,@(cdar clausules)) + (cond ,@(cdr clausules)))))) + +(defun !reduce (func list initial) + (if (null list) + initial + (!reduce func + (cdr list) + (funcall func initial (car list))))) + + +(defun code-char (x) x) +(defun char-code (x) x) +(defvar *newline* (string (code-char 10))) + +(defun concat (&rest strs) + (!reduce (lambda (s1 s2) (concat-two s1 s2)) + strs + "")) + +;;; Concatenate a list of strings, with a separator +(defun join (list separator) + (cond + ((null list) + "") + ((null (cdr list)) + (car list)) + (t + (concat (car list) + separator + (join (cdr list) separator))))) + +(defun join-trailing (list separator) + (if (null list) + "" + (concat (car list) separator (join-trailing (cdr list) separator)))) + +(defun integer-to-string (x) + (if (zerop x) + "0" + (let ((digits nil)) + (while (not (= x 0)) + (push (mod x 10) digits) + (setq x (truncate x 10))) + (join (mapcar (lambda (d) (string (char "0123456789" d))) + digits) + "")))) + +(debug (integer-to-string 323)) -- 1.7.10.4