From e6931993d7193b60881d0303110974b6af6af852 Mon Sep 17 00:00:00 2001 From: Darren Bane Date: Fri, 21 Aug 2020 19:40:30 +0100 Subject: Some more ISLisp->CL examples --- cdbc.lisp | 10 ++++++++++ cfree-fall.lisp | 19 +++++++++++++++++++ doc/bane.20.cdr15.md | 15 ++++++++------- 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 cdbc.lisp create mode 100644 cfree-fall.lisp 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): -- cgit 1.4.1-2-gfad0