add and remove an element to a mathematical set

1k Views Asked by At

I'd like to write pseudo code with mathematical notation, I will write the code phrases in Python. I need to know if

first question:

aList = []
aList.empty() is not True

equals

$aList \neq \emptyset $

second question

a = 5
aList.append(a)

equals

$a \gets 5$

$aList \cup a$

third question

how to write to get the first element and remove it from the list i.e.:

curElement = aList[0]
aList.remove(0)

Thanks for any advice the Internet couldn't help me out.

Kind Regards

1

There are 1 best solutions below

3
On BEST ANSWER

Notice that mathematicians and computer scientists of that field (type system and so on) do have their own shorter notation for these operations. However, they are not that common among the general public.

What are lists?

There are many list like constructs in math. For mathematician they are all functions. Basically, a list/tuple is a function $f$ from $\{1,\dotsc,n\}$ into a set, say $X$. So typically you define the set of lists with values in $X$ as its Kleene star: $$ X^* = \{ f:\{1,\dotsc, n\} \to X \mid n \in\mathbb N \} = \bigcup_{n\in\mathbb N} X^n, $$ where $\mathbb N$ includes $0$.

Most computer scientists and mathematicians in that field use to work with partial functions. So you could also write $$ X^* = \{ f:\mathbb N\to X \mid \operatorname{dom} f = \{1, \dotsc, |\operatorname{dom} f| \} \}. $$ They also identify $f$ with its graph, that is $$ f = \{ (i,f(i)) \mid i\in\operatorname{dom} f \}. $$

Answer to your question:

Using partial functions you can express

  1. "$f\in X^*$ is a empty list" by $f = \emptyset$,
  2. "$g\in X^*$ is $f\in X^*$ appended with $a\in X$" by $$ g = f \cup \{ (|\operatorname{dom} f| + 1, a) \}, $$
  3. "$g\in X^*$ is $f\in X^*$ removed the first element" by $$ g = \{ (i-1, f(i)) \mid i\in \operatorname{dom} f\setminus\{1\} \}. $$