博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第柒章學習 Lisp 3rd Edition, Winston & Horn
阅读量:6520 次
发布时间:2019-06-24

本文共 8376 字,大约阅读时间需要 27 分钟。

hot3.png

ITERATION ON NUMBERS AND LISTS---------------------------------------------------------------------DOTIMES Supports Iteration on Numbers(dotimes <(count parameter> 
) )(defun dotimes-expt (m n) (let ((result 1))                    ;Initialize result parameter. (dotimes (count n result)        ;Evaluate body N times. (setf result (* m result)))))    ;Multiply by M again.題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題7-1: Defines DOTIMES-FACTORIAL, using a DOTIMES form.Factorial of n is 1, if n is 0, and n times the factorial of (n-1) otherwise.That is:n! = n * (n-1)!,    for n>0;n! = 1,            for n=0.---------------------------------------------------------------------------DOLIST Supports Iteration on Lists(dolist (
) )* (count-layers '(18 75 31 180 270 52))3(setf freezing 32 boiling 212)(defun count-layers (list-of-elements) (let ((result 0))                        ;Initialize result parameter.    (dolist (element list-of-elements    ;Establish DOLIST parameter.                    result)               ;Result form.    (when (or (> element boiling)        ;Test element against high.                (< element freezing)       ;Test element against low.        (setf result (+ result 1))))))    ;Increment result.(defun first-n-outlyers (n list-of-elements) (let ((result 0)                         ;Initialize result.        (outlyers nil))                    ;Initialize result. (dolist (element list-of-elements        ;Establish DOLIST parameter.                   outlyers)               ;Result form    (cond ((or (> element boiling)        ;Test element against high.               (< element freezing))       ;Test element against low.           (setf result (+ result 1))    ;Increment result.           (push element outlyers))        ;Add element to the result.          ((= n result) (return outlyers))))))  ;Stop when N found.(defun clear-top (object) (dolist (obstacle
t> (get-rid-of obstacle)))題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題7-2: Write COUNT-OUTYERS-WITH-COUNT-IF using COUNT-IF instead of DOLIST.-------------------------------------------------------------------------7-3: Write LIST-OUTLYERS, a procedure that lists the elements in a list that are below the freezing point of water, FREEZING, or above the boilingpoint, BOILING. While you could do this using REMOVE-IF-NOT, do it insteadusing DOLIST.------------------------------------------------------------------------7-4: Write LIST-OUTLYER-COUNTS, a procedure that counts the temperatures below freezing and above boiling separately, returning a list of the twocounts. While you could do this with two separate calls involvingCOUNT-IF, do it instead using DOLIST.-----------------------------------------------------------------------7-5: Ignoring the existence of MEMBER, write DOLIST-MEMBER, an iterative version of recursive-member:(defun revursive-member (item l) (cond ((endp l) nil) ((eql item (first l)) t) (t (recursive-member item (rest l)))))Note that both RECURSIVE-MEMBER and your solution, unlike MEMBER, are toreutrn T when an item is found.-----------------------------------------------------------------------7-6: Ignoring the existence of Reverse, write DOLIST-REVERSE using DOLIST.-------------------------------------------------------------------------- DO Is More General than DOLIST and DOTIMES(defun do-expt (m n)                (do ((result 1)                        ;Bind parameters.   (exponent n))                    ;Bind parameters. ((zerop exponent) result)        ;Test and reeutrn. (setf result (* m result))           ;Body. (setf exponent (- exponent 1))))     ;Body.(defun do-expt (m n)  (do ((result 1)                       ;Bind parameters.     (exponent n))                    ;Bind parameters.      ()                                ;Test always fails.    (when (zerop exponent)              ;Test.      (return result))                  ;Return.    (setf result (* m result))          ;Body.    (setf exponent (- exponent 1))))    ;Body.(defun do-expt (m n) (do ((result 1 (* m result))           ;Bind and update parameters.     (exponent n (- exponent 1)))      ;Bind and update parameters.    ((zerop exponent) result)))        ;Test and return.(DO ((
) (
) ... (
)) (
) )(defun do-expt (m n) (do ((result m (* m result))    ;Bugged!. (exponent n (- exponent 1))    ;RESULT starts as m not 1. (counter (- exponent 1)    ;Value of COUNTER is one less (- exponent 1)))    ;then value of EXPONENT. ((zerop counter) result)))題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題題7-7: Define D0-FACTORIAL, using a DO form with an empty body.---------------------------------------------------------------------7-8: Ignoring the existence of MEMBER, write DO-MEMBER, an iterative version of RECURSIVE-MEMBER using DO. Like MEMEBER,DO-MEMBER is to return the remainder of the list when the itemis encountered:(defun recursive-member (item 1) (cond ((endp l) nil)    ((eql item (first l)) l) (t (recursive-member item (rest l)))))--------------------------------------------------------------------7-9: Ignoring the existence of REVERSE, a primitive supplied by Lisp, write DO-RESERVE using DO.---------------------------------------------------------------------(LOOP )--------------------------------------------------------------------* (setf cheers '(cheer cheer cheer))* (setf loop-count 0)* (loop    (when (endp cheers) (return loop-count))    (setf cheers (rest cheers))    (setf loop-count (+ loop-count 1)))3-------------------------------------------------------------------* (prog1 (setf a 'x) (setf b 'y) (setf c 'z))X* (progn (setf a 'x) (setf b 'y) (setf c 'z))Z----------------------------------------------------------------------(if
(progn
...
) (progn
...
))解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解7-1:(defun do-times-factorial (n)  (let ((result 1))    (dotimes (count n result)    (setf result (* (+ 1 count) result)))))Note that this version of DOTIMES-FACTORIAL is somewhat inefficient because we have to add one to COUNT each time the body of the DOTIMES form is evaluated. If we did not do this, N would range from 0 ton-1 rather than from 1 to n.----------------------------------------------------------------7-2:(defun count-outlyers-with-count-if (list-of-element) (count-if #'(lambda (element) (or (> element boiling)                                   (< element freezing)))            list-of elements))-----------------------------------------------------------------7-3:(defun list-outlyers (list-of-elements) (let ((outlyers nil))                     ;Initialize result. (dolist (element list-of-elements       ;Establish DOLIST parameter.                     outlyers)              ;Return form.    (when (or (> element boiling)        ;Test element against high.                (< element freezing))       ;Test element against low.        (setf outlyers                      ;Add element to the result.              (cons element outlyers)))))) ;Build result.-------------------------------------------------------------------7-4:(defun list-outlyer-counts (list-of-elements) (let ((count-above 0)                    ;Initialize count parameter. (count-below 0))                   ;Initialize count parameter. (dolist (element list-of-elements    ;Establish DOLIST parameter.                     (list count-below count-above)) ;Return form.    (cond ((> element boiling)           ;Test element against high.             (setf count-above            ;Increment COUNT-ABOVE.                   (+ count-above 1))) ((< element freezing)        ;Test element against low.            (setf count-below            ;Increment count-BELOW.                   (+ count-below 1))))))--------------------------------------------------------------------7-5:(defun dolist-member (item initial-list) (dolist (l initial-list)            ;Step through all elements.    (when (eql item l)      (return t))))--------------------------------------------------------------------7-6:(defun dolist-reverse (initial-list) (let ((result nil)) (dolist (l initial-list result)    ;Step through all elements.      (setf result (cons l result))))) ;Build result using CONS.---------------------------------------------------------------------7-7:(defun do-factorial (n)  (do ((result 1 (* n result))        ;Initialize and reset.     (n n (- n 1)))                ;Initialize and reset. ((zerop n) result)))            ;Test and return.------------------------------------------------------------------7-8:(defun do-member (item initial-list) (do ((l initial-list (rest l)))    ;Step through all elements. ((or (endp l)                  ;List exhausted?           (eql item (first l)))    ;First element EQL to ITEM?       l)))                        ;Return L, which may be NIL.-------------------------------------------------------------------7-9:(defun do-reverse (initial-list) (do ((l initial-list (rest l))             ;Step through elements.       (result nil (cons (first l) result))) ;Build Result.      ((endp l) result)))                    ;Terminate.

转载于:https://my.oschina.net/chuangpoyao/blog/39423

你可能感兴趣的文章
应用程序框架实战三十四:数据传输对象(DTO)介绍及各类型实体比较(转)
查看>>
放量滞涨,抛出信号
查看>>
BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法
查看>>
linux主机下的Vmware Workstation配置NAT设置 端口映射-Ubuntu为例
查看>>
unity physics joint
查看>>
TD的访问地址
查看>>
tmpFile.renameTo(classFile) failed 错误
查看>>
【甘道夫】Apache Hadoop 2.5.0-cdh5.2.0 HDFS Quotas 配额控制
查看>>
一张图看懂normal,static,sealed,abstract 的 区别
查看>>
Task的使用
查看>>
grep和正则表达式
查看>>
s:iterator巧妙控制跳出循环
查看>>
移动互联网思维
查看>>
Serv-U 的升级及数据备份和迁移【转】
查看>>
webstorm无法显示左边文件夹目录的解决方法
查看>>
数字校园-云资源平台 2014.10.26-人人通共享空间
查看>>
为你的网站加上SSL,可以使用HTTPS进行访问
查看>>
软件project--谈项目开发
查看>>
在Android中创建文件
查看>>
爬虫基础
查看>>