From: ················@gmail.com
Subject: Strange error in CLISP
Date: 
Message-ID: <46cd73f5-44b6-498e-9a3a-6d56d5a5adc5@v29g2000hsf.googlegroups.com>
Hi to all Lispers,

I'm a novice, making a utility to help my work. It's basicly about
evaluating numeric data from some physical tests on soil samples. My
main working enviroment is SBCL-Emacs-SLIME (at home), but I'm also
doing it at worktime (if there's nothiing else to do), on CLISP+SLIME.
That's the enviroment I'd use the thing after all.

I have a function called LOCATE to find the highest point of a curve
by evaluating the function of the curve x times in a given range - it
can be a bit slow, usually about 1000 calls. So I wrote QUICKLOCATE,
which uses LOCATE too, but it only looks at a few points in the range,
then focuses on a narrower range around the highest point found, with
finer granularity - this goes on recursively until a given precision
is met. The number of necessary function calls is around x/10. I wrote
some functions to test QUICKLOCATE's precision and tune it's default
arguments. Here they are:

(defun testql (fn)
  "Returns two functions: 1. calling fn and increasing counter;
2. returning a reseting counter."
  (let ((countr 0))
    (values (lambda (arg)
	      (incf countr)
	      (funcall fn arg))
	    (lambda ()
	      (let ((ret countr))
		(setf countr 0)
		ret)))))

(defmacro ql (locate fn pred start stop)
  "Returns LOCATE/QUICKLOCATE results and number of fn. calls."
  `(multiple-value-bind (test reslt)
       (testql #',fn)
     (multiple-value-bind (y x)
	 (,locate test #',pred :start ,start :stop ,stop)
       (list y x (funcall reslt)))))

;; The program uses Lagrange interpolation for the curve. LAGRANGE-FN
takes a list of
;; "coordinates" '((x1 y1) (x2 y2) ... (xn yx)) and returns a function
whick calculates Y
;; values on a curve defined by the coords.
;; WITH-LAGRANGE-FN creates a temporary function to be used in the
body.

(defmacro with-lagrange-fn (l x1 x2 coords &body body)
  "Provides a Lagrange-fn of COORDS for the body. X1 and X2 are
leftmost/rightmost WCONTs."
  `(let ((crds ,coords))
     (let ((,x1 (first (first crds)))
	   (,x2 (first (first (last crds)))))
       ((lambda (,l)
	  ,@body)
	(lagrange-fn crds)))))

(defmacro ql-expr (locate pred coords)
  "Utility for LL_ and QL_."
  `(let ((crds ,coords))
     (with-lagrange-fn l x1 x2 crds
       (ql ,locate l ,pred x1 x2))))

(defmacro ll_ (coords pred)
  "LOCATE result & number of fn. calls."
  `(ql-expr locate ,pred ,coords))

(defmacro ql_ (coords pred)
  "QUICKLOCATE results & number of fn. calls."
  `(ql-expr quicklocate ,pred ,coords))

(defmacro comp_ (coords pred)
  "Prints LOCATE & QUICKLOCATE result."
  (let ((crds coords)
	(prd pred))
    `(let ((ll (ll_ ,crds ,prd))
	   (ql (ql_ ,crds ,prd)))
       (list ll ql))))

So these are pretty simple macros (I'm not finished with it yet, but
one can figure out the speed increment with them). The code works well
on SBCL, but on CLISP I got a this error message for calling COMP_:


CONCATENATE: #1=6.9100003 is not a SEQUENCE
   [Condition of type SIMPLE-TYPE-ERROR]

Restarts:
  0: [ABORT-REQUEST] Abort handling SLIME request.
  1: [ABORT] ABORT

Backtrace:
  0: #<SPECIAL-OPERATOR LET*>
  1: #<SPECIAL-OPERATOR BLOCK>
  2: #<FUNCTION LOAD-MACADAM (HOME FILES) "Loads files from a
directory." (DECLARE (SYSTEM::IN-DEFUN LOAD-MACADAM)) (BLOCK LOAD-
MACADAM (DOLIST (FILE FILES) (LOAD #)))> 2
  3: #<SPECIAL-OPERATOR PROGN>
  4: ..........


6,9100003 is the x coordinate of the first point in the coords list,
but that's it - I'm totally clueless what does it have to do with
CONCATENATE. The available backtraces lead to another source file,
which contains functions for loading the whole program, but I don't
use any of them in the program itself.

Calling QL from the toplevel yields the correct answer. I tried WITH-
LAGRANGE-FN with simple body (LIST X1 X2), that worked also. But if I
try to type the same expression as in QL-EXPR:

 (with-lagrange-fn l x1 x2 c1
       (ql locate l > x1 x2))

it gives me the same error.

Please can anybody tell me what I'm doing wrong?



(sorry for the bad english...)

From: ················@gmail.com
Subject: Re: Strange error in CLISP
Date: 
Message-ID: <513a4246-cd2f-439d-8cae-3697821f8dfa@j78g2000hsd.googlegroups.com>
OK, I find the error. Two of them.

(1) I CALLED a function from that other source file (the function
"l"), only I forgot because the local bind should shadow that one.

(2) But it didn't because in the TESTQL expression

> (defmacro ql (locate fn pred start stop)
>   "Returns LOCATE/QUICKLOCATE results and number of fn. calls."
>   `(multiple-value-bind (test reslt)
>        (testql #',fn)
>      (multiple-value-bind (y x)
>          (,locate test #',pred :start ,start :stop ,stop)
>        (list y x (funcall reslt)))))

there should be no #'.

Sorry for asking this stupid question.
From: Pascal J. Bourguignon
Subject: Re: Strange error in CLISP
Date: 
Message-ID: <7cve5vdtwi.fsf@pbourguignon.anevia.com>
················@gmail.com writes:
> [...incomplete code...]
> Please can anybody tell me what I'm doing wrong?


You didn't provide enough code to reproduce the problem.  
What is LOCATE?  The problem may lie with it.

C/USER2[154]> (macroexpand '(with-lagrange-fn l x1 x2 c1   (ql locate l > x1 x2)))
(LET ((CRDS C1))
 (LET ((X1 (FIRST (FIRST CRDS))) (X2 (FIRST (FIRST (LAST CRDS))))) ((LAMBDA (L) (QL LOCATE L > X1 X2)) (LAGRANGE-FN CRDS)))) ;
T
C/USER2[155]> (macroexpand-1 '(QL LOCATE L > X1 X2))
(MULTIPLE-VALUE-BIND (TEST RESLT) (TESTQL #'L)
 (MULTIPLE-VALUE-BIND (Y X) (LOCATE TEST #'> :START X1 :STOP X2) (LIST Y X (FUNCALL RESLT)))) ;
T


-- 
__Pascal Bourguignon__
·························@anevia.com
http://www.anevia.com
From: ················@gmail.com
Subject: Re: Strange error in CLISP
Date: 
Message-ID: <5de3db80-3351-4a8a-af3a-657c790172ad@m34g2000hsb.googlegroups.com>
On Jan 15, 3:51 pm, ····@anevia.com (Pascal J. Bourguignon) wrote:
> ················@gmail.com writes:
> > [...incomplete code...]
> > Please can anybody tell me what I'm doing wrong?
>
> You didn't provide enough code to reproduce the problem.  
> What is LOCATE?  The problem may lie with it.
>
> C/USER2[154]> (macroexpand '(with-lagrange-fn l x1 x2 c1   (ql locate l > x1 x2)))
> (LET ((CRDS C1))
>  (LET ((X1 (FIRST (FIRST CRDS))) (X2 (FIRST (FIRST (LAST CRDS))))) ((LAMBDA (L) (QL LOCATE L > X1 X2)) (LAGRANGE-FN CRDS)))) ;
> T
> C/USER2[155]> (macroexpand-1 '(QL LOCATE L > X1 X2))
> (MULTIPLE-VALUE-BIND (TEST RESLT) (TESTQL #'L)
>  (MULTIPLE-VALUE-BIND (Y X) (LOCATE TEST #'> :START X1 :STOP X2) (LIST Y X (FUNCALL RESLT)))) ;
> T
>
> --
> __Pascal Bourguignon__
> ·························@anevia.comhttp://www.anevia.com

Here are the searching functions.

(defun locate (fn pred &key (start 0) (stop 18) (step 0.01))
  "Locates extrema of a function."
  (do* ((x (+ start step) (+ x step))
	(y (funcall fn x) (funcall fn x))
	(ex (cons start (funcall fn start))
	    (if (funcall pred y (cdr ex))
		(cons x y)
		ex)))
       ((>= x stop) (values (cdr ex) (car ex)))))

(defun quicklocate (fn pred &key (start 0) (stop 18) (step 0.01)
		    (sections 8) (segment 1/4))
  "Locates extrema of a function quickly."
  (labels ((qloc (start- stop-)
	     (let* ((width (- stop- start-))
		    (swidth (/ width sections)))
	       (multiple-value-bind (y x)
		   (locate fn pred :start start-
			   :stop stop- :step swidth)
		 (if (<= swidth step)
		     (values y x)
		     (let ((segm (* width segment 1/2)))
		       (qloc (mmax start (- x segm))
			     (mmin stop (+ x segm)))))))))
    (qloc start stop)))
From: Maciej Katafiasz
Subject: Re: Strange error in CLISP
Date: 
Message-ID: <fmjil3$ime$1@news.net.uni-c.dk>
Den Tue, 15 Jan 2008 02:54:40 -0800 skrev denes.cselovszky:

[snip]
> (defmacro ql (locate fn pred start stop)
>   "Returns LOCATE/QUICKLOCATE results and number of fn. calls."
>   `(multiple-value-bind (test reslt)
>        (testql #',fn)
>      (multiple-value-bind (y x)
> 	 (,locate test #',pred :start ,start :stop ,stop)
>        (list y x (funcall reslt)))))

You have a lot of macros like that. Why? There's absolutely nothing in 
here you couldn't (and shouldn't) use a function for. Also, "reslt" is 
bad naming, the one-character gain compared to "result" is just not worth 
it.

Cheers,
Maciej
From: ················@gmail.com
Subject: Re: Strange error in CLISP
Date: 
Message-ID: <78fd78a1-d7e6-44a7-b4a7-47121561bad2@d21g2000prf.googlegroups.com>
On jan. 16, 01:20, Maciej Katafiasz <········@gmail.com> wrote:
> Den Tue, 15 Jan 2008 02:54:40 -0800 skrev denes.cselovszky:
>
> [snip]
>
> > (defmacro ql (locate fn pred start stop)
> >   "Returns LOCATE/QUICKLOCATE results and number of fn. calls."
> >   `(multiple-value-bind (test reslt)
> >        (testql #',fn)
> >      (multiple-value-bind (y x)
> >     (,locate test #',pred :start ,start :stop ,stop)
> >        (list y x (funcall reslt)))))
>
> You have a lot of macros like that. Why? There's absolutely nothing in
> here you couldn't (and shouldn't) use a function for. Also, "reslt" is
> bad naming, the one-character gain compared to "result" is just not worth
> it.
>
> Cheers,
> Maciej

You're right, I have to work on my style. Thanks.

csd