lilypond-user
[Top][All Lists]
Advanced

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

Re: Validating a Scheme list


From: Urs Liska
Subject: Re: Validating a Scheme list
Date: Thu, 15 May 2014 10:48:10 +0200
User-agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0

Am 15.05.2014 10:41, schrieb Orm Finnendahl:
Hi Urs,

  it doesn't make too much sense to first collect the whole list and
then check for membership of #f. It'd be more reasonable to stop
iteration once any of the preconditions isn't met.

Ah yes, I didn't mention that this is exactly what I thought too. But didn't manage to do.

You could achieve
this by using a recursive function like below (you could make it
more terse by using if instead of cond but in recursive functions cond
dispatch is more common and I find it more readable...):

  #(define-scheme-function (parser location lst) (list?)
      (letrec ((test (lambda (x)
               (cond ((null? x) #t)
                     (#t (and (list? (car x))
                              (= (length (car x)) 2)
                              (test (cdr x))))))))
       (if (test lst)
             (display "invalid")
             (display "valid"))
       (newline)))

This works perfectly (except that the output is exactly the opposite).
I'll see how to integrate this in the actual context.

Thanks
Urs


Am Donnerstag, den 15. Mai 2014 um 10:09:10 Uhr (+0200) schrieb Urs Liska:
Hi all,

I am working on a Scheme function and would like to check if I have found the 
best solution for a specific subpart. Somehow it looks more complicated than 
necessary.

The function needs to test if each element of a given list is a (sub)list with 
exactly two elements. So

'((1 2)(3 4))

would return #t while

'((5 6)(7 8 9))

would return #f.

My solution is

\version "2.19.6"

validate =

#(define-scheme-function (parser location lst) (list?)

    (if (memv #f (map (lambda sig

                        (and (list? (car sig))

                             (= 2 (length (car sig))))) lst))

        (display "invalid")

        (display "valid"))

    (newline))

{

   \validate #'((1 2)(3 4))

   \validate #'((5 6)(7 8 9))

}

The "framework" doesn't matter, it's just a compilable example. My question is only about 
the "if" expression.

What it does is:
- go through the elements of lst
- produce a list of boolean values,
   - #t if we have a two element list,
   - #f if not
- check if this intermediate list contains at least one #f

Somehow this looks clumsy to me, and I'd like to know (and learn) if there's a 
better solution for this.

TIA
Urs


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user


_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user



--




reply via email to

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