Vector: Difference between revisions

1,795 bytes added ,  3 years ago
added Ol
m (Promote to task, lots of examples, little controversy)
(added Ol)
Line 1,203:
a/2: (2.5, 3.5)
- : unit = ()</pre>
 
=={{header|Ol}}==
Ol has builtin vector type, but does not have built-in vector math.
The vectors can be created directly using function (vector 1 2 3) or from list using function (make-vector '(1 2 3)). Additionally, exists short forms of vector creation: #(1 2 3) and [1 2 3].
<lang scheme>
(define :+ +)
(define (+ a b)
(if (vector? a)
(if (vector? b)
(vector-map :+ a b)
(error "error:" "not applicable (+ vector non-vector)"))
(if (vector? b)
(error "error:" "not applicable (+ non-vector vector)"))
(:+ a b)))
 
(define :- -)
(define (- a b)
(if (vector? a)
(if (vector? b)
(vector-map :- a b)
(error "error:" "not applicable (+ vector non-vector)"))
(if (vector? b)
(error "error:" "not applicable (+ non-vector vector)"))
(:- a b)))
 
(define :* *)
(define (* a b)
(if (vector? a)
(if (not (vector? b))
(vector-map (lambda (x) (:* x b)) a)
(error "error:" "not applicable (* vector vector)"))
(if (vector? b)
(error "error:" "not applicable (* scalar vector)"))
(:* a b)))
 
(define :/ /)
(define (/ a b)
(if (vector? a)
(if (not (vector? b))
(vector-map (lambda (x) (:/ x b)) a)
(error "error:" "not applicable (/ vector vector)"))
(if (vector? b)
(error "error:" "not applicable (/ scalar vector)"))
(:/ a b)))
 
(define x [1 2 3 4 5])
(define y [7 8 5 4 2])
(print x " + " y " = " (+ x y))
(print x " - " y " = " (- x y))
(print x " * " 7 " = " (* x 7))
(print x " / " 7 " = " (/ x 7))
</lang>
{{Out}}
<pre>
#(1 2 3 4 5) + #(7 8 5 4 2) = #(8 10 8 8 7)
#(1 2 3 4 5) - #(7 8 5 4 2) = #(-6 -6 -2 0 3)
#(1 2 3 4 5) * 7 = #(7 14 21 28 35)
#(1 2 3 4 5) / 7 = #(1/7 2/7 3/7 4/7 5/7)
</pre>
 
=={{header|ooRexx}}==