How can I solve this recursion function task?

134 Views Asked by At

I really need help on this task. Im stuck at it and I really would appreciate your help here.

Give a recursive function $r$ on $A$ that reverses a string. For instance, $r(logikk) = kkigol$ and $r(moro) = orom$. (given that $A$ the amount of letters in the Norwegian alphabet which has 29 letters. ). Define the function in such a way that it is correctly regardless of what $A$ are.

$logikk$ means $logic$ in norwegian, and $moro$ means $fun$ in norwegian in case you're wondering.

Edit:

I think i've figured out one of the recursive functions:

$r(\Lambda) = \Lambda$ (empty string)

$r(l) = r(a) + o$

$r(o) = r(l) + g$

$r(g) = r(o) + i$

$r(i) = r(g) + k$

$r(k) = r(i) + k$

$r(k) = r(k)$

Can someone please check if this is correct for $r(logikk)$ I feel like i'm missing something but i'm not sure what.

Thanks a lot for your help!

1

There are 1 best solutions below

10
On BEST ANSWER

HINT: The recursion is on the length of the string. That is, if $w$ is a word and $\alpha$ is a letter, the recursion step of the definition should tell you how to construct $r(w\alpha)$ if you already know $r(w)$. For example, it should tell you that if $r(w)=\text{låm}$, then $r(w\text{e})=\text{elåm}$. (If you prefer nynorsk, you can make that $r(w\text{a})=\text{alåm}$!) You can get the recursion started by defining $r(\epsilon)$, where $\epsilon$ is the empty word.