Fix 12 exercise defects from review wave 1 (scouts a+d)
This commit is contained in:
1 parent
3f753b2a8f
commit
2735c8dca5
18 files changed
+49
-5
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
# 00_intro
|
||||||
|
|
||||||
|
The Lisplings flow, and reading your first error.
|
||||||
|
|
||||||
|
intro1 is error-free: edit it, save it, and watch the exercise run
|
||||||
|
again in the terminal (press n to move on, h for a hint). intro2
|
||||||
|
does not load; read the error in the terminal and fix it so the
|
||||||
|
greeting prints.
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
;;; In Lisp the operator comes first: (+ 1 2) is 3. Nesting needs no
|
;;; In Lisp the operator comes first: (+ 1 2) is 3. Nesting needs no
|
||||||
;;; precedence rules at all: (* 2 (+ 3 4)) is 14.
|
;;; precedence rules at all: (* 2 (+ 3 4)) is 14.
|
||||||
;;;
|
;;;
|
||||||
|
;;; (defun name () ...) just names the expression; chapter 02
|
||||||
|
;;; explains it.
|
||||||
|
;;;
|
||||||
;;; Fix each definition.
|
;;; Fix each definition.
|
||||||
|
|
||||||
;; TODO: Add 10 and 32.
|
;; TODO: Add 10 and 32.
|
||||||
|
|||||||
@@ -4,6 +4,9 @@
|
|||||||
;;;
|
;;;
|
||||||
;;; A Lisp delight: division is exact. (/ 1 2) is the rational 1/2,
|
;;; A Lisp delight: division is exact. (/ 1 2) is the rational 1/2,
|
||||||
;;; not 0.5. Integers, rationals and floats form one number tower.
|
;;; not 0.5. Integers, rationals and floats form one number tower.
|
||||||
|
;;;
|
||||||
|
;;; (defun name () ...) just names the expression; chapter 02
|
||||||
|
;;; explains it.
|
||||||
|
|
||||||
;; TODO: (2 + 3) multiplied by (4 - 1). Should be 15.
|
;; TODO: (2 + 3) multiplied by (4 - 1). Should be 15.
|
||||||
(defun calc-a ()
|
(defun calc-a ()
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
;;; '(+ 1 2) evaluates to the list (+ 1 2)
|
;;; '(+ 1 2) evaluates to the list (+ 1 2)
|
||||||
;;;
|
;;;
|
||||||
;;; 'x is reader shorthand for (quote x).
|
;;; 'x is reader shorthand for (quote x).
|
||||||
|
;;;
|
||||||
|
;;; (defun name () ...) just names the expression; chapter 02
|
||||||
|
;;; explains it.
|
||||||
|
|
||||||
;; TODO: The form itself, not its value.
|
;; TODO: The form itself, not its value.
|
||||||
(defun the-form ()
|
(defun the-form ()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
;;; earlier parameter.
|
;;; earlier parameter.
|
||||||
|
|
||||||
;; TODO: Repeat WORD N times. N is optional and defaults to 2.
|
;; TODO: Repeat WORD N times. N is optional and defaults to 2.
|
||||||
|
;; make-list builds a list of n copies (lists arrive in ch. 06).
|
||||||
(defun repeat-word (word n)
|
(defun repeat-word (word n)
|
||||||
(make-list n :initial-element word))
|
(make-list n :initial-element word))
|
||||||
|
|
||||||
@@ -22,5 +23,6 @@
|
|||||||
(check-equal '("hi" "hi") (repeat-word "hi"))
|
(check-equal '("hi" "hi") (repeat-word "hi"))
|
||||||
(check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4))
|
(check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4))
|
||||||
(check-equal '(:width 2 :height 2 :depth 2) (make-box 2))
|
(check-equal '(:width 2 :height 2 :depth 2) (make-box 2))
|
||||||
|
(check-equal '(:width 3 :height 3 :depth 3) (make-box 3))
|
||||||
(check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5))
|
(check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5))
|
||||||
(check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7))
|
(check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7))
|
||||||
@@ -3,8 +3,9 @@
|
|||||||
;;; A function may return several values at once:
|
;;; A function may return several values at once:
|
||||||
;;; (values 1 2) => 1 and 2
|
;;; (values 1 2) => 1 and 2
|
||||||
;;; Everything except the first value rides along, waiting for
|
;;; Everything except the first value rides along, waiting for
|
||||||
;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. FLOOR is a
|
;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. The checks
|
||||||
;;; classic: (floor 10 3) returns 3 AND 1.
|
;;; below gather the values with MULTIPLE-VALUE-LIST, turning them
|
||||||
|
;;; into a list. FLOOR is a classic: (floor 10 3) returns 3 AND 1.
|
||||||
|
|
||||||
;; TODO: Return TWO values: the whole part of A/B and the rest.
|
;; TODO: Return TWO values: the whole part of A/B and the rest.
|
||||||
(defun divide (a b)
|
(defun divide (a b)
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
;;; LET may reuse a name: the new binding shadows the old one, and
|
;;; LET may reuse a name: the new binding shadows the old one, and
|
||||||
;;; the initializer still evaluates in the outer scope.
|
;;; the initializer still evaluates in the outer scope.
|
||||||
|
|
||||||
;; TODO: The inner X should be the outer X plus 5 - make it so,
|
;; TODO: Keep the outer 15; make the inner binding the outer X
|
||||||
;;; without changing either number.
|
;; plus 5.
|
||||||
(defun shadow-add ()
|
(defun shadow-add ()
|
||||||
(let ((x 15))
|
(let ((x 15))
|
||||||
(let ((x 15))
|
(let ((x 15))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
;;;
|
;;;
|
||||||
;;; SETF assigns to any "place": a variable, a list element, a
|
;;; SETF assigns to any "place": a variable, a list element, a
|
||||||
;;; property. The general shape is (setf PLACE new-value).
|
;;; property. The general shape is (setf PLACE new-value).
|
||||||
|
;;; (getf *account* :balance) reads the property back.
|
||||||
|
|
||||||
(defparameter *account* (list :owner "Ada" :balance 100))
|
(defparameter *account* (list :owner "Ada" :balance 100))
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
;;; A single colon reaches only EXPORTED symbols. The kitchen
|
;;; A single colon reaches only EXPORTED symbols. The kitchen
|
||||||
;;; makes soup; only the soup is on the menu.
|
;;; makes soup; only the soup is on the menu.
|
||||||
|
|
||||||
|
;; TODO: The defpackage below is missing (:export #:make-soup).
|
||||||
(defpackage #:kitchen
|
(defpackage #:kitchen
|
||||||
(:use #:common-lisp))
|
(:use #:common-lisp))
|
||||||
|
|
||||||
|
|||||||
@@ -24,3 +24,4 @@
|
|||||||
|
|
||||||
(check-equal "yum" (catch-empty '(a b)))
|
(check-equal "yum" (catch-empty '(a b)))
|
||||||
(check-equal "soup" (catch-empty '()))
|
(check-equal "soup" (catch-empty '()))
|
||||||
|
(check-error (eat '()) 'empty-plate)
|
||||||
@@ -31,3 +31,8 @@
|
|||||||
(check-equal 1 (car-with-default '(1 2 3)))
|
(check-equal 1 (car-with-default '(1 2 3)))
|
||||||
(check-equal nil (car-with-default "not a list"))
|
(check-equal nil (car-with-default "not a list"))
|
||||||
(check-equal nil (car-with-default nil))
|
(check-equal nil (car-with-default nil))
|
||||||
|
(check-equal nil
|
||||||
|
(handler-bind ((error (lambda (e)
|
||||||
|
(declare (ignore e))
|
||||||
|
(invoke-restart (find-restart 'use-nil)))))
|
||||||
|
(safe-car "not a list")))
|
||||||
@@ -24,3 +24,6 @@
|
|||||||
(check (eql 0 (current-depth)))
|
(check (eql 0 (current-depth)))
|
||||||
(check (eql 3 (let ((*depth* 3)) (current-depth))))
|
(check (eql 3 (let ((*depth* 3)) (current-depth))))
|
||||||
(check (eql 0 (current-depth)))
|
(check (eql 0 (current-depth)))
|
||||||
|
(check (eql 42 (let ((*depth* 42))
|
||||||
|
(with-depth 7 (lambda () nil))
|
||||||
|
(current-depth))))
|
||||||
@@ -14,5 +14,6 @@
|
|||||||
(check-equal '("hi" "hi") (repeat-word "hi"))
|
(check-equal '("hi" "hi") (repeat-word "hi"))
|
||||||
(check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4))
|
(check-equal '("hi" "hi" "hi" "hi") (repeat-word "hi" 4))
|
||||||
(check-equal '(:width 2 :height 2 :depth 2) (make-box 2))
|
(check-equal '(:width 2 :height 2 :depth 2) (make-box 2))
|
||||||
|
(check-equal '(:width 3 :height 3 :depth 3) (make-box 3))
|
||||||
(check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5))
|
(check-equal '(:width 2 :height 5 :depth 2) (make-box 2 :height 5))
|
||||||
(check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7))
|
(check-equal '(:width 2 :height 5 :depth 7) (make-box 2 :height 5 :depth 7))
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
;;; FUNCTIONS 4: MULTIPLE VALUES (solution)
|
;;; FUNCTIONS 4: MULTIPLE VALUES (solution)
|
||||||
|
;;;
|
||||||
|
;;; The checks below gather the values with MULTIPLE-VALUE-LIST,
|
||||||
|
;;; turning them into a list.
|
||||||
|
|
||||||
(defun divide (a b)
|
(defun divide (a b)
|
||||||
(floor a b))
|
(floor a b))
|
||||||
|
|||||||
@@ -20,3 +20,4 @@
|
|||||||
|
|
||||||
(check-equal "yum" (catch-empty '(a b)))
|
(check-equal "yum" (catch-empty '(a b)))
|
||||||
(check-equal "soup" (catch-empty '()))
|
(check-equal "soup" (catch-empty '()))
|
||||||
|
(check-error (eat '()) 'empty-plate)
|
||||||
@@ -21,3 +21,8 @@
|
|||||||
(check-equal 1 (car-with-default '(1 2 3)))
|
(check-equal 1 (car-with-default '(1 2 3)))
|
||||||
(check-equal nil (car-with-default "not a list"))
|
(check-equal nil (car-with-default "not a list"))
|
||||||
(check-equal nil (car-with-default nil))
|
(check-equal nil (car-with-default nil))
|
||||||
|
(check-equal nil
|
||||||
|
(handler-bind ((error (lambda (e)
|
||||||
|
(declare (ignore e))
|
||||||
|
(invoke-restart (find-restart 'use-nil)))))
|
||||||
|
(safe-car "not a list")))
|
||||||
@@ -18,3 +18,6 @@
|
|||||||
(check (eql 0 (current-depth)))
|
(check (eql 0 (current-depth)))
|
||||||
(check (eql 3 (let ((*depth* 3)) (current-depth))))
|
(check (eql 3 (let ((*depth* 3)) (current-depth))))
|
||||||
(check (eql 0 (current-depth)))
|
(check (eql 0 (current-depth)))
|
||||||
|
(check (eql 42 (let ((*depth* 42))
|
||||||
|
(with-depth 7 (lambda () nil))
|
||||||
|
(current-depth))))
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
(defun eval-expr (expr)
|
(defun eval-expr (expr)
|
||||||
(cond ((numberp expr) expr)
|
(cond ((numberp expr) expr)
|
||||||
((and (consp expr) (eq 'quote (car expr)))
|
((and (consp expr) (eq 'quote (car expr)))
|
||||||
(second expr))
|
(car (cdr expr)))
|
||||||
((and (consp expr) (member (car expr) '(+ - *)))
|
((and (consp expr) (member (car expr) '(+ - *)))
|
||||||
(apply (car expr) (mapcar #'eval-expr (cdr expr))))
|
(apply (car expr) (mapcar #'eval-expr (cdr expr))))
|
||||||
(t (error "cannot evaluate ~s" expr))))
|
(t (error "cannot evaluate ~s" expr))))
|
||||||
|
|||||||
Reference in new issue
Block a user