在LispWorks中进行编程28:练习答案
Category:UI界面编写练习答案
并非所有练习都有答案。
清单
1。
(list 1 (list 2 (list 3 4)))
表达式
定义程序
1.一个数字
(defun square (number) (* number number))
2.找到第n个三角形数字
(defun triangular (n) (/ (* n (+ n 1)) 2))
3.找到扔两个骰子的结果
(defun two-dice () (+ (+ 1 (random 6)) (+ 1 (random 6))))
变量
1.在千米和英里之间转换
(defparameter kilometresinmiles 0.621371192) (defun convert-km (km) (* km kilometresinmiles)) (defun convert-miles (miles) (/ miles kilometresinmiles))
2.找到三个数字的平均值
(defun average3 (number1 number2 number3) (/ (+ number1 number2 number3) 3))
3.立方体两个数字的总和
(defun cubesum (a b) (let* ((total (+ a b)) (answer (* total total total))) answer))
4.将值替换为二次方程
(defun pseudo-primes (x) (+ (- (* x x) x) 41))
操纵列表
1.交换列表中的前两项
(defun swap (lst) (cons (second lst) (cons (first lst) (rest (rest lst)))))
2.复制列表中的第一项
(defun dup (lst) (cons (first lst) lst))
3.从列表中返回随机项
(defun random-elt (lst) (nth (random (length lst)) lst))
4.返回列表中的最后一项
(defun last-elt (lst) (nth (- (length lst) 1) lst))
字符串
1.翻转单词的中间字母
(defun midverse (word) (concatenate ‘string (subseq word 0 1) (reverse (subseq word 1 (- (length word) 1))) (subseq word (- (length word) 1))))
或不使用长度:
(defun midverse (word) (concatenate ‘string (subseq word 0 1) (subseq (reverse (subseq word 1)) 1) (subseq (reverse word) 0 1)))
2. 将字符串向左旋转n个位置
(defun rotate (str places) (concatenate ‘string (subseq str places) (subseq str 0 places)))
打印
测试结果
1.测试弦是否是回文
2.测试对象是否是两个数字的列表
3.写一个piglatin翻译器
(defun piglatin (word) (let* ((initial (subseq word 0 1))) (if (or (string= initial “a”) (string= initial “e”) (string= initial “i”) (string= initial “o”) (string= initial “u”)) (concatenate ‘string word “way”) (concatenate ‘string (subseq word 1) (subseq word 0 1) “ay”))))
创建对话框
写作课程
处理列表中的项目
1.计算列表中的元素数量
2.反转字符串列表中的每个字符串
3.查找列表中的每个数字是偶数还是奇数
4.找到列表的最大元素
(defun max-list (lst) (if (null (rest lst)) (first lst) (let* ((maxrest (max-list (rest lst)))) (if (> (first lst) maxrest) (first lst) maxrest))))
5.复制列表中的每个元素
(defun dupli-list (lst) (if (null lst) nil (append (list (first lst) (first lst)) (dupli-list (rest lst)))))
6.消除列表中的连续重复项
(defun compress (lst) (if (null lst) nil (if (eq (first lst) (first (rest lst))) (compress (rest lst)) (cons (first lst) (compress (rest lst))))))
7.交错两个列表
(defun interleave (one two) (if (null one) nil (cons (first one) (cons (first two) (interleave (rest one) (rest two))))))
重复操作
有关递归的更多信息
1.计算树上的项目
2.在树上找到一个项目
(defun find-tree (tree word) (if (null tree) nil (if (string= (first tree) word) t (if (find-tree (second tree) word) t (find-tree (third tree) word)))))
3.找到第n个斐波纳契数
(defun fib (n) (if (< n 3) 1 (+ (fib (- n 1)) (fib (- n 2)))))
4.在Pascal的三角形上找到指定的数字
(defun pascal (n r) (if (= n 1) 1 (if (= n r) 1 (+ (pascal (- n 1) (- r 1)) (pascal n (- r 1))))))
推广程序
1.对一系列数字重复一个过程
(defun repeat-for (from to function) (if (> from to) nil (progn (funcall function from) (repeat-for (+ from 1) to function))))
2.使用二元运算符组合数字列表
(defun combine (function lst) (if (null (third lst)) (funcall function (first lst) (second lst)) (funcall function (first lst) (combine function (rest lst)))))
要么:
(defun combine (function lst) (funcall function (first lst) (if (null (third lst)) (second lst) (combine function (rest lst)))))
http://mip.i3geek.com