(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
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
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
(define (dot-product U V)
(gmk u U)
(gmk v V)
(do ((s 0 (+ s (* (u) (v)))))
((unull?) s)))
(dot-product (vector 1 2 3) (vector 4 5 6))
===>
32
Given:
;; Make a generator.
(define (gmk* vec)
(let ((v vec)
(i 0))
(values
(lambda()
(if (= i (vector-length v)) #f (begin0 (~ v i) (inc! i))))
(lambda() (= i (vector-length v))))))
(define-macro gmk
(lambda (sym vec)
`(define-values
(,sym ,(symbol-append sym 'null?))
(gmk* ,vec))))
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
(defun list-of-length (n list)
"Predicate which tests whether LIST is a list
of length N."
(loop for i from 0 below n
when (null list) do (return nil)
do (pop list)
finally (return (null list))))
(defun list-of-length (n list)
"Predicate which tests whether LIST is a list
of length N."
(loop for i from 0 below n
when (null list) do (return nil)
do (pop list)
finally (return (null list))))
Instead of using a macro whose source measures 60 kilobytes,
we can make it shorter by simply using recursion. Of course,
that's not possible in CL since it's not a Lispy language and
doesn't offer tail-call optimization.
Scheme:
(define (list-of-length? n lst)
(cond ((zero? n) (null? lst))
((null? lst) #f)
(#t (list-of-length? (- n 1) (cdr lst)))))
Scheme offers very little; most serious work done with Scheme picks an implementation and uses it.
Many of your own posts pick a particular Scheme like Gauche and often
use its extensions that are not in Scheme.
Linked lists have poor cache performance. Chasing down a linked list
involved dependent loads which the machine cannot prefetch, unlike
marching down an array that is contiguously laid out in memory.
The battle you are trying to pick here is outdated.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 56:26:14 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,806 |