0

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!

Dabbish
  • 521
  • 2
  • 7
  • 15

2 Answers2

4

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.

Brian M. Scott
  • 616,228
  • please check my edit and see if i've done one of them correctly – Dabbish Oct 13 '13 at 18:28
  • 1
    @Dabbish: I’m afraid that you’ve badly misunderstood what $r$ is supposed to do. The function $r$ is supposed to reverse the letters in the input word, so $r(\text{g})=\text{g}$, $r(\text{o})=\text{o}$, and in fact if $\alpha$ is any letter, then $r(\alpha)=\alpha$. Thus, your $r$ is giving the wrong results. It’s also completely tied to the specific word logikk, when it’s supposed to work on any possible input. Your base is correct, but for the recursive step you’ll actually need only one line: for any word $w$ and letter $\alpha$, $r(w\alpha)=$ ... what? – Brian M. Scott Oct 13 '13 at 18:40
  • hmm, what about this

    $\Lambda =$ The empty string, $r(\Lambda) =$ $\Lambda$, $r(k) = k$, $r(k) = k$, $r(i) = i$, $r(g) = g$, $r(o) = o$, $r(l) = l$ would that be more correct? I am not so sure about the on line part but i'll try , $r(wa) = wa$

    – Dabbish Oct 13 '13 at 20:55
  • 1
    @Dabbish: Your definition need not have any provisions for single letters. The base case is $r(\Lambda)=\Lambda$. Then there is a one-line recursion step that of the form $r(w\alpha)=\text{something}$. – Brian M. Scott Oct 13 '13 at 20:58
  • still can't figure i. I would appreciate it if you could give me an answer. I think i'll learn much better out from that then. – Dabbish Oct 13 '13 at 22:25
  • @Dabbish: Let’s try once more; if you don’t get it from this question, I’ll simply give you the answer. When you add a letter to the righthand end of $w$, what happens to the reversal? You add that same letter ... where? – Brian M. Scott Oct 13 '13 at 22:30
  • You add that same letter to the left hand? $r(wa) = r(aw)$? – Dabbish Oct 13 '13 at 22:33
  • 1
    @Dabbish: Yes, and no, in that order. You want $r(w\alpha)=\alpha r(w)$ for any word $w$ and letter $\alpha$. The idea is to say how to construct the reversal of the longer word if you already know the reversal of the shorter one. – Brian M. Scott Oct 13 '13 at 22:35
  • Doh! Almost, hehe. Really, thanks a lot for you time and patience, I appreciate it. – Dabbish Oct 13 '13 at 22:36
  • @Dabbish: You’re welcome. – Brian M. Scott Oct 13 '13 at 22:38
0

You can use recursion here.

Write $A^*$ for the set of all words in the alphabet $A$ (ie finite sequences of elements of $A$). Write $A^{\leqslant n}$ for the set of all words in the alphabet $A$ of length $\leqslant n$. $A*$ is a monoid for the concatenation $\star$ defined by : $$ (u_i)_{i\leqslant n} \star (v_j)_{j \leqslant m} := (u_1, \dots, u_n, v_1, \dots, v_m)$$

Say you have a function $f_n : A^{\leqslant n} \mapsto A^{\leqslant n}$ that reverses all words of length $\leqslant n$. Then you can define $f_{n+1} : A^{\leqslant n+1} \mapsto A^{\leqslant n+1}$ as follow for $\mathbf{u} = (u_i)_{i \leqslant m}$ :

$$ f_{n+1}(\mathbf{u}) := \left\lbrace \begin{array}{cl} f_n(\mathbf{u}) & \textrm{ if } m\leqslant n\\ u_{n+1}\star f_n\big((u_1,\dots, u_n) & \textrm{ if } m = n+1\end{array} \right.$$

By definition, $f_{n+1}$ coincides with $f_n$ on $A^{\leqslant n}$. Now you can define (thr graph of) your function $reverse: A^* \mapsto A^*$ as follow : $$ reverse := \bigcup_{n \in \mathbb{N}} f_n $$

Olivier Roche
  • 5,319
  • 9
  • 16