Hello,
Suppose we have a simple record type for points:
(define-record-type point (fields x y))
Here's a procedure definition for adding two points:
(define (a+b a b)
(make-point (+ (point-x a)
(point-x b))
(+ (point-y a)
(point-y b))))
Now let's use concatenative combinators:
(define point+
(bi2 (bi@ point-x +)
(bi@ point-y +)
make-point))
Add a point and a number:
(define (point+n a n)
(make-point (+ (point-x a) n)
(+ (point-y a) n)))
Concatenative:
(define point+n
(bi2 (dip point-x +)
(dip point-y +)
make-point))
Let's introduce 'sq':
(define sq (dup *))
Standard 'norm':
(define (norm p)
(sqrt (+ (sq (point-x p))
(sq (point-y p)))))
Concatenative:
(define norm
(bi point-x point-y (bi@ sq (uni2 + sqrt))))
Normalize:
(define (normalize p)
(point/n p (norm p)))
Concatenative:
(define normalize
(bi identity norm point/n))
Where 'point/n' is similar to 'point+n' above:
(define point/n
(bi2 (dip point-x /)
(dip point-y /)
make-point))
and 'identity' is:
(define (identity x) x)
Traditional dot product:
(define (dot a b)
(+ (* (point-x a)
(point-x b))
(* (point-y a)
(point-y b))))
Concatenative:
(define dot
(bi2 (bi@ point-x *)
(bi@ point-y *)
+))
Ed
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk