Jump to content

Display a linear combination: Difference between revisions

Added EchoLisp
(=={{header|Racket}}== implementation added)
(Added EchoLisp)
Line 22:
9) -1, -2, 0, -3
10) -1</pre>
 
=={{header|EchoLisp}}==
<lang scheme>
;; build an html string from list of coeffs
 
(define (linear->html coeffs)
(define plus #f)
(or*
(for/fold (html "") ((a coeffs) (i (in-naturals 1)))
(unless (zero? a)
(set! plus (if plus "+" "")))
(string-append html
(cond
((= a 1) (format "%a e<sub>%d</sub> " plus i))
((= a -1) (format "- e<sub>%d</sub> " i))
((> a 0) (format "%a %d*e<sub>%d</sub> " plus a i))
((< a 0) (format "- %d*e<sub>%d</sub> " (abs a) i))
(else ""))))
"0"))
(define linears '((1 2 3)
(0 1 2 3)
(1 0 3 4)
(1 2 0)
(0 0 0)
(0)
(1 1 1)
(-1 -1 -1)
(-1 -2 0 -3)
(-1)))
(define (task linears)
(for/string ((linear linears))
(string-append
(format "%a -> <span style='color:blue'>%a</span> <br>" linear (linear->html linear)))))
</lang>
{{out}}
(1 2 3) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> + 3*e<sub>3</sub> </span> <br>(0 1 2 3) -> <span style='color:blue'> e<sub>2</sub> + 2*e<sub>3</sub> + 3*e<sub>4</sub> </span> <br>(1 0 3 4) -> <span style='color:blue'> e<sub>1</sub> + 3*e<sub>3</sub> + 4*e<sub>4</sub> </span> <br>(1 2 0) -> <span style='color:blue'> e<sub>1</sub> + 2*e<sub>2</sub> </span> <br>(0 0 0) -> <span style='color:blue'>0</span> <br>(0) -> <span style='color:blue'>0</span> <br>(1 1 1) -> <span style='color:blue'> e<sub>1</sub> + e<sub>2</sub> + e<sub>3</sub> </span> <br>(-1 -1 -1) -> <span style='color:blue'>- e<sub>1</sub> - e<sub>2</sub> - e<sub>3</sub> </span> <br>(-1 -2 0 -3) -> <span style='color:blue'>- e<sub>1</sub> - 2*e<sub>2</sub> - 3*e<sub>4</sub> </span> <br>(-1) -> <span style='color:blue'>- e<sub>1</sub> </span> <br>
 
=={{header|J}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.