about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDarren Bane <darren.bane@emdalo.com>2020-08-21 19:40:30 +0100
committerDarren Bane <darren.bane@emdalo.com>2020-08-21 19:40:30 +0100
commite6931993d7193b60881d0303110974b6af6af852 (patch)
treec6ace92a2e93aa13f3b44d32439d93fd29bfaacb
parent5c4dfb5d3e84be9f7e4b9ea8383fcb9de70862aa (diff)
downloadlsp-e6931993d7193b60881d0303110974b6af6af852.tar.gz
Some more ISLisp->CL examples
-rw-r--r--cdbc.lisp10
-rw-r--r--cfree-fall.lisp19
-rw-r--r--doc/bane.20.cdr15.md15
3 files changed, 37 insertions, 7 deletions
diff --git a/cdbc.lisp b/cdbc.lisp
new file mode 100644
index 0000000..9d790ab
--- /dev/null
+++ b/cdbc.lisp
@@ -0,0 +1,10 @@
+;; Ported from https://rosettacode.org/wiki/Assertions_in_design_by_contract#Eiffel
+;; Arguably not *quite* design-by-contract, but very close in vanilla CL
+(defun average-of-absolutes (values)
+  (declare (list values))
+  (assert (> (length values) 0))
+  (let ((res (/ (reduce #'+ (mapcar #'abs values)) (length values))))
+    (assert (>= res 0))
+    (the fixnum res)))
+;; (average-of-absolutes '(1 3))
+;; (average-of-absolutes '())
diff --git a/cfree-fall.lisp b/cfree-fall.lisp
new file mode 100644
index 0000000..412b082
--- /dev/null
+++ b/cfree-fall.lisp
@@ -0,0 +1,19 @@
+;; TODO: air resistance, terminal velocity
+(defconstant +G+ 9.81)
+(defvar *alt* 20000)
+(defvar *v* 0)
+(defun one-step ()
+  (setq *alt* (- *alt* *v*))
+  (setq *v* (+ *v* +G+))
+  (format *standard-output* "~A~%" *alt*))
+(defun my-main ()
+  (loop while (> *alt* 0)
+        do (one-step)))
+
+;;; And here's an example from GNAT:
+(defun free-fall ()
+  (format *standard-output* "Gravitational constant: ~A~%" +G+)
+  (format *standard-output* "Twice that: ~A~%" (* 2 +G+))
+  (let* ((time 10)
+         (distance (* 0.5 +g+ (expt time 2))))
+    (format *standard-output* "Distance travelled in 10 seconds of free fall: ~A~%" distance)))
diff --git a/doc/bane.20.cdr15.md b/doc/bane.20.cdr15.md
index 36f07b5..d27fd47 100644
--- a/doc/bane.20.cdr15.md
+++ b/doc/bane.20.cdr15.md
@@ -20,13 +20,14 @@ and indeed it is only moderate work to port between them
 
 Write an ISLisp program, making the following adaptations:
 
-| ISLisp            | CL                |
-| ----------------- | ----------------- |
-| for               | do                |
-| quotient          | /                 |
-| create            | make-instance     |
-| (class x)         | (find-class 'x)   |
-| (standard-output) | *standard-output* |
+| ISLisp            | CL                  |
+| ----------------- | ------------------- |
+| (class x)         | (find-class 'x)     |
+| create            | make-instance       |
+| for               | do                  |
+| quotient          | /                   |
+| (standard-output) | *standard-output*   |
+| (while t b)       | (loop while t do b) |
 
 ISLisp doesn't have the following features (and probably many more):