|
From: | Nicolas Sceaux |
Subject: | Re: [PATCH] Re: where to put useful lists in the docs? |
Date: | Sat, 30 May 2009 11:31:47 +0200 |
Le 30 mai 09 à 10:20, Mark Polesky a écrit :
Here's another useful set of procedures that is not in lily-library.scm. These functions make quick work of calculating offsets, extents, bounding-boxes, etc.Here's a new patch that also incorporates conditionals. ;; defines 14 functions: ;; pair_+ pair_- pair_* pair_/ ;; pair_= pair_< pair_> pair_<= pair_>= ;; pair_gcd pair_lcm pair_min pair_max pair_average. ;; eg. (pair_+ '(1 . 1) '(2 . 3) '(5 . 8)) ==> (8 . 12) ;; (pair_<= '(1 . 8) '(1 . 5) '(2 . 3)) ==> (#t . #f) ;; (pair_min '(1 . 8) '(1 . 5) '(2 . 3)) ==> (1 . 3) (map primitive-eval (map (lambda (f) `(define-public (,(symbol-append 'pair_ f) . pairs) (apply (lambda (f . pairs) ((lambda (lst) (cons (car lst) (cadr lst))) (primitive-eval (append `(map ,f) (map (lambda (x) `(list ,(car x) ,(cdr x))) pairs))))) ,f pairs))) '(+ - * / = < > <= >= gcd lcm min max average)))
This is not the way to do it in guile scheme. When you find yourself using primitive-eval, ask yourself if there is not a better way. To write code that writes codes, use macros: read about defmacro. Also note that you usually don't use underscores in function names. But in that particular case, why not define a function that takes a function and a list of pairs? It's usual to have procedure arguments: (define (map-on-pairs fn pairs) (cons (apply fn (map car pairs)) (apply fn (map cdr pairs)))) (map-on-pairs + '((1 . 1) (2 . 3) (5 . 8))) => (8 . 12) (map-on-pairs min '((1 . 8) (1 . 5) (2 . 3))) ==> (1 . 3) nicolas
[Prev in Thread] | Current Thread | [Next in Thread] |