Adding vectors (Prolog)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Java | Prolog | Python

Vector addition


\overrightarrow{u} + \overrightarrow{v}
=
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
+
\begin{bmatrix} 
v_1 & v_2 & ... & v_n
\end{bmatrix}
=
\begin{bmatrix} 
u_1 + v_1 & u_2 + v_2 & ... & u_n + v_n
\end{bmatrix}

<<vector_addition.pl>>=
vadd([], [], []).
vadd([H1|T1], [H2|T2], [H3|T3]) :-
    H3 is H1 + H2,
    vadd(T1, T2, T3).
Download code
hijacker
hijacker
hijacker
hijacker