Skip to content

Commit

Permalink
Functional common lisp version.
Browse files Browse the repository at this point in the history
  • Loading branch information
owainkenwayucl committed Dec 12, 2017
1 parent 6380722 commit 2183187
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clisp_func_pi_dir/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sbcl:
PI_ARG=$(PI_ARG) make -f Makefile.sbcl run

clean:
rm -f pi.fas pi.lib pi.o
8 changes: 8 additions & 0 deletions clisp_func_pi_dir/Makefile.sbcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CLISP=sbcl
CLOPTS=--control-stack-size 256 --script

run:
@$(CLISP) $(CLOPTS) pi.cl $(PI_ARG)

clean:
rm -f pi.fas pi.lib pi.o
2 changes: 2 additions & 0 deletions clisp_func_pi_dir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**Note:** for stack size reasons this version only currently works on SBCL.

54 changes: 54 additions & 0 deletions clisp_func_pi_dir/pi.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
; PI Common LISP example.

; Abstract getting command line arguments.
; In sbcl the interpreter is an extra argument.

; For CLISP: clisp pi.cl <n>
; For SBCL: sbcl --script pi.cl <n>
; For GCL: gcl -f pi.cl <n>
; For ECL: ecl -shell pi.cl <n>

(defun getargs ()
(or
#+CLISP *args*
#+SBCL (cdr *posix-argv*)
#+GCL (cdr si::*command-args*)
#+ECL (cdr(cdr(cdr si::*command-args*)))
nil))

(defun range (b n j)
(if (>= b n)
'()
(cons b (range (+ b j) n j))
)
)


(defun pc (c) (/ (apply #'+ (map 'list #'(lambda (x) (/ 4 (+ 1 (* x x))))((lambda (n) (range (/ 0.5 n) 1 (/ 1 n))) c))) c))

(defvar n 5000000)

(if (> (list-length(getargs)) 0)
(setq n (parse-integer (car (getargs))))
)

(format t "Calculating PI using~%")
(format t " ~a slices~%" n)
(format t " 1 process~%")

; get-internal-real-time gets time from some arbitrary, implementation
; dependent starting point in some fraction of a second.
(defvar start (get-internal-real-time))

(defvar p (pc n))

; get-internal-real-time is in some platform specific unit which we can find in
; internal-time-units-per-second
(defvar stop (get-internal-real-time))

(defvar runt (/ (- stop start) internal-time-units-per-second))

(format t "Obtained value of PI: ~a~%" p)

; We have to print the time with ~f to force a float output.
(format t "Time taken: ~f seconds~%" runt)
12 changes: 12 additions & 0 deletions clisp_func_pi_dir/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# Detect which if any Common LISP implementation we have:
which sbcl > /dev/null 2>&1
sbclsuccess=$?

if [ "$sbclsuccess" == "0" ]; then
PI_ARG=$1 make -f Makefile.sbcl
else
echo "No Common LISP implementation found."
exit 1
fi

0 comments on commit 2183187

Please sign in to comment.