This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: StackOverflowError in a specialized map


> From: Damien MATTEI <Damien.Mattei@unice.fr>
> Date: Tue, 21 Mar 2017 15:00:57 +0100
> 
> yes, thank you, but i try to stay in a functional programming style
> and avo= id "loop" that make code unreadable and hard to debug,

I suspect I may be misunderstanding your quest, but I don't see a
significant difference between a 'named let' and a nested function
definition (with respect to being functional programming).  Perhaps I
should have used a name 'recur' instead of 'loop' in my earlier version.

For example, if we wish to avoid named let altogether for some reason,
we could use the following variant of my earlier implementation.

(define (map/remove-nulls-1 proc . lsts)
  (define (f lsts result)
    (if (any null? lsts)
        (reverse result)
        (f (map cdr lsts)
           (let ((proc-result (apply proc
                                     (map car lsts))))
             (if (null? proc-result)
                 result
                 (cons proc-result
                       result))))))
  (f lsts '()))

Or, going another way, we could use fold:

(define (map/remove-nulls-2 proc . lsts)
  (reverse (apply fold
                  (lambda fargs
                    (let ((accum (last fargs))
                          (pargs (drop-right fargs 1)))
                      (let ((r (apply proc pargs)))
                        (if (null? r)
                            accum
                            (cons r accum)))))
                  '()
                  lsts)))

As before, I'm using some SRFI 1 procedures for convenience; they can be
avoided easily: any, fold, last, drop-right.  

Both the above versions, like the one I posted earlier, work without
problems in Kawa without needing the --full-tailcalls option (i.e., Kawa
properly detects and eliminates and simple cases of tail calls they
use).  The slight awkwardness of last/drop-right is due to the

But, as I noted before, perhaps I'm missing the main point of the
original question.

Regards,

-chaw



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]