home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!agate!peoplesparc.Berkeley.EDU!fateman
- From: fateman@peoplesparc.Berkeley.EDU (Richard Fateman)
- Newsgroups: sci.math.symbolic
- Subject: Re: Rootterdam Competition in Mathematica Journal
- Date: 16 Nov 1992 19:39:52 GMT
- Organization: University of California, Berkeley
- Lines: 85
- Message-ID: <1e8te8INNaa3@agate.berkeley.edu>
- References: <BxtJGM.472@news.cso.uiuc.edu>
- NNTP-Posting-Host: peoplesparc.berkeley.edu
-
-
- I assume that mathematica competitions are not open to
- LISP solutions, but the split program is rather trivial in
- Common Lisp.
- Here's the most obvious program ..
-
-
- ;;Rotterdam competition
-
- #| requires (split '(a b c d e f g) '(2 0 3 1 1)) -->
- ((a b) () (c d e) (f) (g))
- |#
-
- (defun split (lis parts)
- (if lis (cons (subseq lis 0 (car parts))
- (split (nthcdr (car parts) lis)
- (cdr parts)))))
-
-
- ;;.......
- For a problem resembling Richard Gaylord's,
- it seems that interpreted lisp on a Sparc1+ takes about 0.467
- sec and that compiled lisp takes about 0.017 seconds. (Averaging it
- over 10 runs shows the latter number is probably somewhere in the
- range of 15-20 milliseconds.)
-
- My conclusion: lisp is neater for this task, it seems to be
- quite fast (a factor of 100, compiled? I don't know what machine
- Richard Gaylord was using). Furthermore, I expect that any
- reasonably experienced lisp programmer would come to about the
- same solution, and would find it rather readable.
-
-
- (for non-lispers, cdr=rest, car=first, cons= construct a list,
- subseq creates a list from a subsequence, nthcdr takes n cdrs.)
- .............
-
- The testing code...
-
-
- (setq r (make-array 500))
- #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL ...)
-
- (dotimes (k 500)(setf (aref r k)(random 6)))
- NIL
-
- (reduce #'+ r)
- 1312
-
- (setf lis (coerce (make-array 1312) 'list))
- (NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL ...)
-
- (setf r (coerce r 'list))
- (5 1 3 1 4 0 4 1 5 3 ...)
-
- (time (split lis r))
- cpu time (non-gc) 467 msec user, 117 msec system
- cpu time (gc) 0 msec user, 0 msec system
- cpu time (total) 467 msec user, 117 msec system
- real time 700 msec
- space allocation:
- 10820 cons cells, 0 symbols, 68992 other bytes,
- ((NIL NIL NIL NIL NIL) (NIL) (NIL NIL NIL) (NIL) (NIL NIL NIL NIL) NIL
- (NIL NIL NIL NIL) (NIL) (NIL NIL NIL NIL NIL) (NIL NIL NIL) ...)
-
- (proclaim '(optimize speed (safety 0)))
- T
-
- (compile 'split)
- SPLIT
- NIL
- NIL
-
- (time (split lis r))
- cpu time (non-gc) 17 msec user, 0 msec system
- cpu time (gc) 0 msec user, 0 msec system
- cpu time (total) 17 msec user, 0 msec system
- real time 30 msec
- space allocation:
- 1814 cons cells, 0 symbols, 32 other bytes,
- ((NIL NIL NIL NIL NIL) (NIL) (NIL NIL NIL) (NIL) (NIL NIL NIL NIL) NIL
- (NIL NIL NIL NIL) (NIL) (NIL NIL NIL NIL NIL) (NIL NIL NIL) ...)
- --
- Richard J. Fateman
- fateman@cs.berkeley.edu 510 642-1879
-