p-link

Welcome to John McCarthy's new website.

Posted by jgrant, 2012-04-02 18:10:44



From the website :

John was a legendary computer scientist at Stanford University who developed time-sharing, invented LISP, and founded the field of Artificial Intelligence.

In March 2011 John launched Project JMC with the objective to make his work more approachable and accessible. The Project JMC team is continuing to help realize his objective. In this site you will find all John's work, including his social commentary, and acknowledgements of his outstanding contributions and impact. Additional comments, suggestions, stories, photographs and videos on John and his work are very welcome. Please send them to the Project JMC team.



His old website is here.


p-link

Quick Sort in Shen

Posted by jgrant, 2012-04-02 14:28:39


A Shen type-checked implementation of Quick Sort is even more elegant/terse compared with the CL version posted previously.
Pattern-matching and currying make this possible.

(tc +)

(define filter
  {(A --> boolean) --> (list A) --> (list A)}
  _  []      -> []
  T? [A | B] -> (append [A] (filter T? B)) where (T? A)
  T? [_ | B] -> (filter T? B))

(define quick-sort-generic
  {(list A) --> (A --> A --> boolean) --> (A --> A --> boolean) --> (list A)}
  [] _ _ -> []
  [A | B] L? R? -> (append (quick-sort-generic (filter (R? A) B) L? R?)
			   [A]
			   (quick-sort-generic (filter (L? A) B) L? R?)))

\* descending with duplicates *\
(quick-sort-generic [3 1 2 7 9 6 6 3 0] >= <)
\* [9 7 6 6 3 3 2 1 0] : (list number) *\

\* descending unique numbers *\
(quick-sort-generic [3 1 2 7 9 6 6 3 0] > <)
\* [9 7 6 3 2 1 0] : (list number) *\

\* ascending by length *\
(quick-sort-generic [[2 1 6] [] [1 1 9 0] [0]] 
		(/. X Y (<= (length X) (length Y)))
		(/. X Y (> (length X) (length Y))))
\* [[] [0] [2 1 6] [1 1 9 0]] : (list (list number)) *\

The complete code can be found here. Based on this numeric version.


p-link

Quick Sort in Common Lisp

Posted by jgrant, 2012-03-30 17:05:16


After watching some of Tim Roughgarden's videos on sorting algorithms, I thought I'd post an implementation of quick sort in Common Lisp as an example of a sorting algorithm implemented in CL. It's a simple enough example(at < 20 LOC) that demonstrates one non-imperative approach to algorithm implementation. The complete code can be found here.

(defun quick-sort-generic2 (sequence cfun &optional result-type)
  (if (<= (length sequence) 1)
      (copy-seq sequence)
      (flet ((partition (fun array)
	       (list (remove-if-not fun array) (remove-if fun array))))
	(let* ((result-type (or result-type 'vector))
	       (pivot-ind (random (length sequence)))
	       (pivot-val (elt sequence pivot-ind))
	       (rem-seq
		(remove pivot-val sequence :start pivot-ind :end (+ 1 pivot-ind)))
	       (part (partition (lambda (x) 
				  (apply cfun (list x pivot-val))) rem-seq)))
	  (concatenate result-type
		       (quick-sort-generic2 (car part) cfun result-type) 
		       (list pivot-val)
		       (quick-sort-generic2 (cadr part) cfun result-type))))))

* (test-sort)

started quick-sort (generic, array) ...
Evaluation took:
  0.089 seconds of real time
  0.081912 seconds of total run time (0.081587 user, 0.000325 system)
  92.13% CPU
  142,664,472 processor cycles
  8,375,024 bytes consed
  
quick-sorted 10000 items (first 10 shown) : 
#(9998 9998 9998 9997 9997 9996 9995 9994 9993 9992) 

started quick-sort (generic, list) ...
Evaluation took:
  0.062 seconds of real time
  0.058722 seconds of total run time (0.058417 user, 0.000305 system)
  95.16% CPU
  99,419,648 processor cycles
  9,371,456 bytes consed
  
quick-sorted 10000 items (first 10 shown) : 
(9999 9998 9997 9997 9996 9996 9994 9993 9993 9992) 



p-link

Happy Pi Day in Shen

Posted by jgrant, 2012-03-14 22:26:23


Here's a port of the previous Qi II code to Shen.
Run with Hakan Raberg's 0.1.4 version of shen.clj (Shen implemented in Clojure !).

\*
  Accurately calculates N digits of Pi using Machin's formula
  with fixed point arithmetic and variable guards digits. 

  Depends on the maths library -->
    http://www.shenlanguage.org/library.html
*\

(tc +)

(define arccot-
  {number --> number --> number --> number --> number --> number} 
  X N XPOWER    0 _ -> 0
  X N XPOWER TERM 1 -> (+ (arccot- X (+ N 2) (floor (/ XPOWER X)) 
                                     (floor (/ XPOWER N)) 0) 
                          (floor (/ XPOWER N)))
  X N XPOWER TERM 0 -> (- (arccot- X (+ N 2) (floor (/ XPOWER X))
                                      (floor (/ XPOWER N)) 1) 
                          (floor (/ XPOWER N))))

(define arccot
  {number --> number --> number}
  X UNITY -> (let XPOWER (floor (/ UNITY X))
                  (arccot- (* X X) 1 XPOWER (floor XPOWER) 1)))

(define machin-pi
  {number --> number} 
  DIGITS -> (let GUARD (+ 10 (ceiling (log' DIGITS 10)))
                 UNITY (expt 10 (+ DIGITS GUARD))
                 (floor (/ (* 4 (- (* 4 (arccot 5 UNITY))
                                   (arccot 239 UNITY)))
                           (expt 10 GUARD)))))




(1+) (time (machin-pi 100))

run time: 2.56899999999996 secs
31415926535...4350265344N : number


p-link

Robot readable world

Posted by jgrant, 2012-02-25 16:58:02


How do robots see the world? How do they gather meaning from our streets, cities, media and from us?
This is an experiment in found machine-vision footage, exploring the aesthetics of the robot eye.



p-link

Smile or Die (AKA Mandatory Optimism)

Posted by jgrant, 2012-02-25 16:30:58


Barbara Ehrenreich explores the darker side of positive thinking.