#| ;; $Id: examples.lisp,v 1.1 2003/10/21 17:30:56 nhabedi Exp $ ;; EXAMPLES.LISP ;; Nick Levine, Ravenbrook Limited, 2003-08-14 ;; ;; These are the examples I expect to use in the tutorial on CLOS ;; at the International Lisp Conference 2003. ;; ;; This document is mainly for my operational convenience. You might ;; want to raid fragments to help you get started when building CLOS ;; into your Common Lisp applications. Nothing useful will happen if ;; you try to cl:load this document into a lisp image. ;; ;; This document is provided "as is", without any express or implied ;; warranty. In no event will the author be held liable for any ;; damages arising from the use of this document. You may make and ;; distribute verbatim copies of this document provided that you do ;; not charge a fee for this document or for its distribution. |# ; #+WAM-CL (prolog-call "cls.") (defun mapcar-visualize (func l) (if (null l) () (cons (apply func (list (first l))) (mapcar func (rest l))))) (in-package "CL-USER") ' (load "sanity-util") '(require 'sanity-util) (write-line "Running smoke test!") ; (progn (prolog-inline "rtrace") (is eq 1 1)) (is eq 1 1) (is equal (list 1 'a 'b) (cons 1 '(a b))) (is eq 2 (if nil 1 2)) (is eq t (= 1.0 1)) (is eq nil (equal 1.0 1)) (is equal '(3 2 1) (destructuring-bind (a b &optional (c 3) ) '(1 2)(LIST c b a)(LIST c b a) )) (is equal '(3 2 1) (destructuring-bind (a b &key (c 3) ) '(1 2 :c 3)(LIST c b a)(LIST c b a) )) (is eq t (keywordp :k)) (is eq 10 (if t 10 20)) (is eq t (stringp "abc")) ;; "FAI "this has ben fix" LED: when matching ~a and ~a~%", ['$CHAR'(b), '$CHAR'(c)], "bc", t). (is equal (subseq "abc" 1) "bc") (is eq 1 (if t 1 2)) (is eq 2 (if nil 1 2)) (defun fib (n) (if (> n 1) (+ (fib (- n 1)) (fib (- n 2))) 1)) (disassemble #'fib) (is eql 89 (fib 10)) (defun accum (r) (if (= 0 r) (list 0) (cons r (accum (- r 1))))) (disassemble #'accum) #| DISASSEMBLY FOR:f_u_accum :- dynamic f_u_accum/2. f_u_accum(A, G) :- ( 0=:=A -> G=[0] ; C is A - 1, f_u_accum(C, D), G=[A|D] ). |# (is equal (list 4 3 2 1 0) (accum 4)) (defmacro defwrap (name) `(defun ,name () 1)) ;;; :- ensure_loaded('sanity-test.lisp.trans.pl'). (defwrap foo) (is eq 1 (foo)) (is equal (macroexpand-1 '(defwrap foo)) '(defun foo nil 1)) (write-line "PASSED") (defun fifteen () (let (val) (tagbody (setq val 1) (go point-a) (incf val 16) point-c (incf val 04) (go point-b) (incf val 32) point-a point-u ;; unused (incf val 02) (go point-c) (incf val 64) point-b (incf val 08)) val)) (disassemble #'fifteen) #| /* this first one should get deleted since its inlined away in f_u_fifteen */ addr_tagbody_1_addr_enter_1(Env10) :- symbol_setter(Env10, setq, u_val, 1), addr_tagbody_1_u_point_a(Env10). addr_tagbody_1_u_point_c(Incf_Env) :- place_op(Incf_Env, incf, [value, u_val], [4], Incf_R), addr_tagbody_1_u_point_b(Incf_Env). addr_tagbody_1_u_point_a(Incf_Env19) :- place_op(Incf_Env19, incf, [value, u_val], [2], Incf_R18), addr_tagbody_1_u_point_c(Incf_Env19). addr_tagbody_1_u_point_u(Incf_Env23) :- place_op(Incf_Env23, incf, [value, u_val], [2], Incf_R22), addr_tagbody_1_u_point_c(Incf_Env23). addr_tagbody_1_u_point_b(Incf_Env27) :- place_op(Incf_Env27, incf, [value, u_val], [8], _GORES15). f_u_fifteen(MResult) :- Env=[], catch(( TBEnv=[[bv(u_val, [])]|Env], symbol_setter(TBEnv, setq, u_val, 1), addr_tagbody_1_u_point_a(TBEnv), symbol_value(TBEnv, u_val, U_val_Get), U_val_Get=MResult ), block_exit(u_fifteen, MResult), true). |# (is eq 15 (fifteen)) (defun do-four () (DO ((temp-one 1 (1+ temp-one) )(temp-two 0 (1- temp-two) ) )((> (- temp-one temp-two) 5) temp-one)() )) (is = 4 (do-four)) (is eq 'string_l (DEFUN string_l (x )(COND ((STRINGP x )x )((SYMBOLP x )(symbol-name x ))(T (ERROR "type error" ))))) (is eq () (TAGBODY 1 (PRINT "hi" ))) (is eq () (TAGBODY a (PRINT "hi" ))) (is eq () (LET ((val 1 ))NIL )) (is eq () (LET ((val 1 )) )) (is eql 1 (LET ((val 1 ))val )) (is eql 'world (LET ((a 'b) )(LET ((a 'world) ) (LET ((a 'hello) )(LET ((a a)(*package* (find-package :keyword) ) )(PRINT a) ) )(PRINT a) ) ))