Hello lispers!
I've written a toy program to count the number of lines of a given stream,
using tail recursion. It runs fine with sbcl but core dumps with clisp.
Does that mean that clisp doesn't optimise for tail recursion, or am I
missing something? (I wasn't able to figure this out from clisp's docs)
(defun create-line-counter (file)
(defun count-lines (&optional (counter 0))
(if (read-line file nil nil) (count-lines (1+ counter)) counter)))
(let ((logfile (open "access_log" :direction :input)))
(format t "~d" (funcall (create-line-counter logfile))))
Thanks for reading,
Serafeim
Serafeim <······@it.teithe.gr> writes:
> I've written a toy program to count the number of lines of a given stream,
> using tail recursion. It runs fine with sbcl but core dumps with clisp.
> Does that mean that clisp doesn't optimise for tail recursion, or am I
> missing something? (I wasn't able to figure this out from clisp's docs)
>
> (defun create-line-counter (file)
> (defun count-lines (&optional (counter 0))
> (if (read-line file nil nil) (count-lines (1+ counter)) counter)))
>
> (let ((logfile (open "access_log" :direction :input)))
> (format t "~d" (funcall (create-line-counter logfile))))
Until you compile the function, indeed no tail-call optimization is
node in clisp interpreter. This comes handy when you try to debug it...
Try to insert: (compile 'create-line-counter) between the two above forms.
(And core dumps shouldn't occur, have you compiled it with libsegv?)
--
__Pascal Bourguignon__ http://www.informatimago.com/
NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
Pascal Bourguignon wrote:
> Until you compile the function, indeed no tail-call optimization is
> node in clisp interpreter. This comes handy when you try to debug it...
>
> Try to insert: (compile 'create-line-counter) between the two above forms.
You're right! (And I'm impressed with the amazingly fast response :)
> (And core dumps shouldn't occur, have you compiled it with libsegv?)
I'm using the default package for ubuntu.
Thanks!
Serafeim
Serafeim <······@it.teithe.gr> writes:
> Pascal Bourguignon wrote:
>
>> Until you compile the function, indeed no tail-call optimization is
>> node in clisp interpreter. This comes handy when you try to debug it...
>>
>> Try to insert: (compile 'create-line-counter) between the two above forms.
>
> You're right! (And I'm impressed with the amazingly fast response :)
This is just random. Internet is worldwide. Whatever the time, there
will always be someone awake and reading the news ready to answer.
>> (And core dumps shouldn't occur, have you compiled it with libsegv?)
>
> I'm using the default package for ubuntu.
My advice is to always download the (latest) sources and compile them
yourself, for software you really use.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"A TRUE Klingon warrior does not comment his code!"