1 Feb 08:11
Re: segfault with filter
<Manuel.Serrano <at> inria.fr>
2012-02-01 07:11:40 GMT
2012-02-01 07:11:40 GMT
Hi,
> Tue, 31 Jan 2012 14:51:29 +0100, cyprien.nicolas wrote:
> > According to gdb, it's a stack overflow. filter consumes stack as it
> > must check if a element must be kept or not in the newly allocated
> > resulting list.
> >
> > You can find the definition of filter in runtime/Ieee/control.scm.
>
> Would it make sense to replace the implementation of such a popular function
> with one that is tail-recursive?
> Or even by a dumb version like the following:
>
> (define (filter pred l)
> (let loop ((l l)
> (new '()))
> (cond ((null? l)
> (reverse! new))
> ((pred (car l))
> (loop (cdr l) (cons (car l) new)))
> (else
> (loop (cdr l) new)))))
Filter, tries, as much as possible to return the same list as its argument.
That is:
1:=> (define l '(1 2 3))
1:=> (equal? l (filter (lambda (x) #t) l)
--> #t
This implies that it is not tail-recursive, contrary to filter! which is
(Continue reading)
RSS Feed