lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Make some local functions public (was: Re: lily-library.scm


From: Joe Neeman
Subject: Re: [PATCH] Make some local functions public (was: Re: lily-library.scm question)
Date: Mon, 15 Jun 2009 13:03:32 +0300



On Mon, Jun 8, 2009 at 3:59 AM, Mark Polesky <address@hidden> wrote:
my version:

(define-public (split-at-predicate predicate lst)
 "Split LST (into 2 lists) at the first element that returns #f for
  (PREDICATE previous_element element), and return the 2 new lists as a
  pair. Example: (split-at-predicate < '(1 2 3 2 1)) ==> ((1 2 3) 2 1)"
 (if (< (length lst) 2)
     (cons lst '())
     (let loop ((L0 (list (car lst))) (L1 (cdr lst)))
       (cond ((null? L1) (cons L0 L1))
             ((predicate (car (last-pair L0))

last-pair is an O(n) operation, since it has to traverse the whole list, making split-at-predicate O(n^2) instead of O(n), as it should be. You'd be better off building L0 backwards and reversing it at the end, so that the element you need is always at the beginning of a list.

Joe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]