Sponsored links: Algebra eBooks
 

Related

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-let

:lisp (lisp-implement...

:lisp (apropos 'carte...

:lisp (defun cartesia...

Calculate

labels-lambda-length-let

:lisp (list-all-packa...

:lisp (length (list-a...

:lisp (defun search-p...

Calculate

labels

Run Example
(%i1):lisp (defun r (n) (labels ((f (k l z) (let ((ln (+ (* 1.003 l) (* 0.006 z))) (zn (+ (* 0.0069 l) (* 1.026 z)))) (if (<
 k n) (f (1+ k) ln zn) (cons (/ ln l) (/ zn z)))) (f 0 10 1000))))) (print (r 100));

NIL 
NIL
(%i1) 
Run Example
:lisp (defun enum-interval (low high &optional p)  (let* ((result nil)         (end-result 0))    (do* ((low low (+ low 1)))      ((>
 low high) (if (not p) end-result (values-list end-result)))      (setf end-result (reverse (push low result))))));

ENUM-INTERVAL
(%i1) :lisp (defun group (source n)  (if (zerop n) (error "zero length"))  (labels ((rec (source acc)             (let ((rest (nthcdr n source)))               (if (consp rest)                   (rec rest (cons (subseq source 0 n) acc))                   (nreverse (cons source acc))))))    (if source (rec source nil) nil)));

GROUP
(%i1) :lisp (defun row-sum (min max n)  (let ((list (enum-interval min max)) (result))    (values-list     (dotimes (i (length (group list n)) (values (nreverse result)))       (push        (list (mapcan (lambda (x) (if (numberp x) (list x)))              (nth i (group list n)))              (format nil "sum: ~a" (reduce '+         (mapcan (lambda (x) (if (numberp x) (list x)))        (nth i (group list n))))))        result)))));

ROW-SUM
(%i1) :lisp (defun col-sum (min max n)  (let ((list (enum-interval min max)) (result))    (values-list     (dotimes (i (1+ (length (group list n))) (values (nreverse result)))       (push        (list (mapcan (lambda (x) (if (numberp x) (list x))) (mapcar (lambda (x) (nth i x)) (group list n)))              (format nil "sum: ~a" (reduce '+ (mapcan (lambda (x) (if (numberp x) (list x))) (mapcar (lambda (x) (nth i x)) (group list n))))))        result)))));

COL-SUM
(%i1) :lisp (row-sum 0 10 5);

((0 1 2 3 4) sum: 10)
((5 6 7 8 9) sum: 35)
((10) sum: 10)
(%i1) :lisp (row-sum 0 10 2);

((0 1) sum: 1)
((2 3) sum: 5)
((4 5) sum: 9)
((6 7) sum: 13)
((8 9) sum: 17)
((10) sum: 10)
(%i1) :lisp (col-sum 0 10 5);

((0 5 10) sum: 15)
((1 6) sum: 7)
((2 7) sum: 9)
((3 8) sum: 11)
(%i1) :lisp (col-sum 0 10 2);

((0 2 4 6 8 10) sum: 30)
((1 3 5 7 9) sum: 25)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(%i1) :lisp (row-sum 0 10 11);

((0 1 2 3 4 5 6 7 8 9 10) sum: 55)
(%i1) :lisp (row-sum 1 10 10);

((1 2 3 4 5 6 7 8 9 10) sum: 55)
(%i1) :lisp (col-sum 0 10 1);

((0 1 2 3 4 5 6 7 8 9 10) sum: 55)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(%i1) :lisp (col-sum 1 10 1);

((1 2 3 4 5 6 7 8 9 10) sum: 55)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(NIL sum: 0)
(%i1) :lisp (defun accumulate (op init seq)  (if (null seq)      init      (funcall op (car seq)               (accumulate op init (cdr seq)))));

ACCUMULATE
(%i1) :lisp (defun horner-eval (x coeff-seq)  (accumulate (lambda (this-coeff higher-terms)                (+ (* higher-terms x) this-coeff))              0              (reverse coeff-seq)));

HORNER-EVAL
(%i1) :lisp (horner-eval 0 (list 3 2 1));

1
(%i1) f(x):=(3*(x^2))+(2*x)+1;
                                       2
(%o1)                       f(x) := 3 x  + 2 x + 1
(%i2) f(0);
(%o2)                                  1
(%i3) :lisp (horner-eval 2 (list 3 2 1));

17
(%i3) f(2);
(%o3)                                 17
(%i4) :lisp (defun entry (tree) (car tree))(defun left-branch (tree) (cadr tree))(defun right-branch (tree) (caddr tree))(defun make-tree (entry left right)  (list entry left right));

MAKE-TREE
(%i4) :lisp (defun element-of-set? (x set)  (cond ((null set) false)        ((= x (entry set)) true)        ((<
 x (entry set))         (element-of-set? x (left-branch set)))        ((>
 x (entry set))         (element-of-set? x (right-branch set)))));

ELEMENT-OF-SET?
(%i4) :lisp (defun adjoin-set (x set)  (cond ((null set) (make-tree x '() '()))        ((= x (entry set)) set)        ((<
 x (entry set))         (make-tree (entry set)                     (adjoin-set x (left-branch set))                    (right-branch set)))        ((>
 x (entry set))         (make-tree (entry set)                    (left-branch set)                    (adjoin-set x (right-branch set))))));

ADJOIN-SET
(%i4) :lisp (defun partial-tree (elts n)  (if (= n 0)      (cons '() elts)      (let ((left-size (quotient (- n 1) 2)))        (let ((left-result (partial-tree elts left-size)))          (let ((left-tree (car left-result))                (non-left-elts (cdr left-result))                (right-size (- n (+ left-size 1))))            (let ((this-entry (car non-left-elts))                  (right-result (partial-tree (cdr non-left-elts)                                              right-size)))              (let ((right-tree (car right-result))                    (remaining-elts (cdr right-result)))                (cons (make-tree this-entry left-tree right-tree)                      remaining-elts))))))));

PARTIAL-TREE
(%i4) :lisp (defun list->
tree (list)  (car (partial-tree list (length list))));

LIST->TREE
(%i4) :lisp (defun tree->
list (tree)  (if (null tree)      '()      (append (tree->
list (left-branch tree))              (cons (entry tree)                    (tree->
list (right-branch tree))))));

TREE->LIST
(%i4) :lisp (list->
tree '(1 3 5 7 9 11));

(5 (1 NIL (3 NIL NIL)) (9 (7 NIL NIL) (11 NIL NIL)))
(%i4) :lisp (tree->
list (list->
tree '(1 3 5 7 9 11)));

(1 3 5 7 9 11)
(%i4) 
Run Example
:lisp (defun emit-+ (x y)  (cond ((eql x 0) y)        ((eql y 0) x)        ((and (equal x y)              (not (symbolp x)))         `(let ((y ,x))            (+ y y)))        (t         `(+ ,x ,y))));

EMIT-+
(%i1) :lisp (defun emit-* (x y)  (cond ((or (eql x 0) (eql y 0)) 0)        ((eql y 1) x)        ((eql x 1) y)        ((and (equal x y)              (not (symbolp x)))         `(let ((y ,x))            (* y y)))        (t         `(* ,x ,y))));

EMIT-*
(%i1) :lisp (defun emit-power (x n)  (cond ((= n 0) 1)        ((= n 1) x)        ((oddp n) `(* ,x ,(emit-power x (- n 1))))        (t (emit-square (emit-power x (/ n 2)))))):lisp (defun fold-left (op init seq)  (labels ((iter (result rest)              (if (null rest)                  result                  (iter (funcall op result (car rest))                        (cdr rest)))))  (iter init seq)));

FOLD-LEFT
(%i1) :lisp (defun emit-poly-value (coeffs x)  (let ((* #'emit-*) (+ #'emit-+))    (fold-left (lambda (value coeff)             (+ (* value x) coeff))           0           coeffs))):lisp (emit-poly-value '(5 0 1) 2);

21
(%i1) 

Related Help

Help for Labels