Skip to content
Uri edited this page Jul 12, 2021 · 14 revisions

Basic definitions

(import (otus algebra))

Warning: Otus Lisp is a functional language, so use impure functions (that contains "!" sign in the name) only if you really need to. In any case, the use of impure functions is very limited.

Vectors

Vector is a one-dimensional array. Vector can be declared using several ways:

  • by it's values which can be any applicable number:
> [1 2 3 4 5]
#(1 2 3 4 5)

> [0 -3 3/7 16+4i 7.12 (inexact 7.12) 123456789876543213546576666777757575757444]
#(0 -3 3/7 16+4i 178/25 7.12 123456789876543213546576666777757575757444)
  • As a copy of existing vector:
> (define v [1 2 3 4 5])
> (print v)
#(1 2 3 4 5)
> (copy v)
#(1 2 3 4 5)
  • As an uninializied vector of N elements.
> (vector 14)
#(0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  • As a vector of 0s:
> (zeros 14)
#(0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  • As a vector of 1s:
> (ones 42)
#(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
  • As a vector of an any applicable same value:
> (fill (vector 17) -33)
#(-33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33 -33)

Matrices

A matrix is a rectangular array of numbers or a vector of a vectors in other words.

Matrix can be declared using several ways:

  • by it's values which can be any applicable number:
> [[1 2 3]
   [4 5 6]
   [7 8 9]]
#(#(1 2 3) #(4 5 6) #(7 8 9))
  • As a copy of existing matrix:
> (define v [[1 2 3][4 5 6][7 8 9]])
> (print v)
#(#(1 2 3) #(4 5 6) #(7 8 9))
> (copy v)
#(#(1 2 3) #(4 5 6) #(7 8 9))
  • As a matrix of N same vectors.
> (matrix [1 2 3] 4)
#(#(1 2 3) #(1 2 3) #(1 2 3) #(1 2 3))
  • As an uninializied matrix of rows*columns elements.
> (matrix 3 4)
#(#(0 0 0 0) #(0 0 0 0) #(0 0 0 0))
  • As a matrix of 0s:
> (zeros 3 7)
#(#(0 0 0 0 0 0 0) #(0 0 0 0 0 0 0) #(0 0 0 0 0 0 0))
  • As a matrix of 1s:
> (ones 1 3)
#(#(1 1 1))
  • As a matrix of an any applicable same value:
> (fill (matrix 2 3) -1)
#(#(-1 -1 -1) #(-1 -1 -1))
  • As a matrix with zeros (ones) with same shape of other matrix:
> (define m (matrix 2 5))
> (print m)
#(#(0 0 0 0 0) #(0 0 0 0 0))
> (ones m)
#(#(1 1 1 1 1) #(1 1 1 1 1))
Clone this wiki locally