What does this recursive function do?

48 Views Asked by At

Let $\sum = \{0, 1\}$. Define $f:\sum^* \rightarrow \sum^*$ (where $\sum^*$ is the set of all words of all lengths including $0$) recursively as follows:

$f(\lambda) = 1$ (where $\lambda$ is the empty word)

$f(0w) = 1w$

$f(1w) = 0f(w)$

So I'm unsure how this function maps a given input to an output. For example, what would be the result of $f(10101)$? Does the function first call itself on $f(1w)$ since the first character of the input is $1$ (where $w$ is now the rest of the word: $0101$)?

1

There are 1 best solutions below

0
On BEST ANSWER

For your example:

$$f(1\underline{0101})=0f(\underline{0\overline{101}})=01\overline{101}$$

Essentially we replace all leading $1$'s with $0$ and then replace the first $0$ with a $1$. If there are no $0$'s, we append a $1$ onto the end.