From Newsgroup: comp.lang.lisp
B. Pym wrote:
Steven E. Harris wrote:
(defun dot-product (u v)
(loop for elem-u across u
for elem-v across v
summing (* elem-u elem-v)))
(dot-product (vector 1 2 3)
(vector 4 5 6))
32
Gauche Scheme
(use gauche.sequence)
(define (dot-product u v)
(fold
(lambda (e-u e-v sum) (+ sum (* e-u e-v)))
0
u
v))
(dot-product #(1 2 3) #(4 5 6))
===>
32
Gauche Scheme
(use gauche.sequence) ;; So that "fold" can handle vectors.
(define (dot-product u v)
(% fold (+ (* A: B:) C:) 0 u v))
(dot-product (vector 1 2 3) (vector 4 5 6))
===>
32
Given:
;; Anaphoric macro to abreviate lambdas for higher-order functions.
;; Uses "_" for 1 argument;
;; uses "A:" and "B:" and so on for 2 or more arguments.
;; This version works under both Gauche Scheme
;; and Racket. Racket needs:
;; (require compatibility/defmacro)
;;
(define-macro %
(case-lambda
((func expr List) `(,func (lambda(_) ,expr) ,List))
((func expr . more)
(let* ((n 64)
(nums (map (lambda _ (set! n (+ 1 n)) n) more))
(vnames
(map (lambda(n)
(string->symbol (string (integer->char n) #\:)))
nums)))
`(,func (lambda ,vnames ,expr) ,@more)))))
--
[T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
--- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2