0

I would need some help on how I can define a recursive function $f$ on $\{a, b\}$*

Define a recursive function $f$ on $\{a, b\}$* which replaces any $a$ with $b$ and vice versa, for example, $f(aba) = bab$ and $f(aaabbb) = bbbaaa$

I would appreciate hints and/or examples or atleast how I need to think to solve a task like this one.

Thank YOU.

Dabbish
  • 521
  • 2
  • 7
  • 15

1 Answers1

2

HINT: It’s actually more similar than you might think to this question. If $w\in\{a,b\}^*$, and you know what $f(w)$ is, what are $f(wa)$ and $f(wb)$ in terms of $f(w)$?

Brian M. Scott
  • 616,228
  • for any word $w$ and letter $a$, $b$, $f(awb)$ = $abf(w)$ would that be correct? – Dabbish Oct 13 '13 at 23:50
  • 1
    @Dabbish: That rule would apply only to words beginning with $a$ and ending with $b$, so it could not, for instance, tell you what $f(b)$ and $f(ba)$ are, and it definitely would not do what you want even when it does apply to an input: $f(awb)$ must certainly begin with $b$, not $a$. Once again, if you know what $f(w)$ is, how can you find $f(wa)$? – Brian M. Scott Oct 13 '13 at 23:58
  • Finding $f(wa)$, wouldn't that be the same as the previous question? I would say $af(w)$. So I believe you would have $f(bwa) = af(w)$ ? – Dabbish Oct 14 '13 at 00:04
  • 1
    @Dabbish: Suppose that $w=abb$, so that $f(w)=baa$; according to your rule $f(abba)=abaa$; is that actually correct? // You do not want to consider $f(bwa)$ or $f(awb)$. Every word in ${a,b}^*$ can be built from shorter ones simply by appending $a$ or $b$, and the definition of this particular function does not require looking at anything more complicated than $wa$ and $wb$. – Brian M. Scott Oct 14 '13 at 00:10
  • Would this be correct $f(bw) = f(wa)$?. or would the answer actually be $wa$ and $wb$? – Dabbish Oct 14 '13 at 00:42
  • 1
    @Dabbish: $f(bw)=f(wa)$ doesn’t tell you what $f(bw)$ or $f(wa)$ actually is if all you know is $f(w)$. You’ve got to express $f(wa)$ and $f(wb)$ in terms of $f(w)$. And when you right down a candidate for the recurrence, test it: see whether it actually defines the desired function. Go back to the reversal function for a moment. The rule $r(w\alpha)=\alpha r(w)$ (together with the initial condition $r(\Lambda)=\Lambda$ allows us to compute $r(w)$ for any word $w$: $$r(\text{mål})=\text{l},r(\text{må})=\text{lå},r(\text{m})=\text{låm}, r(\Lambda)=\text{låm},\Lambda=\text{låm};.$$ – Brian M. Scott Oct 14 '13 at 00:51
  • 1
    You want a rule here that in similar fashion lets you calculate $f(w)$ in terms of the way that $w$ is built up from shorter words. – Brian M. Scott Oct 14 '13 at 00:53
  • And how would you actually proceed to make that kind of a rule? Would appreciate if you could answer that. I'm trying to understand the best of how actually recursive functions work. Having a bit difficult time right now. I think i don't understand the very basic of how you can proceed to solve a task or make a rule for a recursive function. The textbook is very confusing. – Dabbish Oct 14 '13 at 01:02
  • 1
    @Dabbish: I suspect that you’re right about missing the basic idea. The idea of a recursive definition is similar to the idea of a proof by induction. You show how to construct a few basic objects, and then you show how to construct all more complicated objects from simpler ones. Here you know that $f(\Lambda)$ should be $\Lambda$: there are no letters to be changed. Now suppose that I told you that I had in mind some $w\in{a,b}^*$ such that the last three letters of $f(w)$ were $abb$; what would the last four letters of $f(wa)$ be? – Brian M. Scott Oct 14 '13 at 01:09
  • would the last four letters be $abba$? – Dabbish Oct 14 '13 at 01:24
  • 1
    @Dabbish: No, the last four letters would be $abbb$: $f$ changes every $a$ to a $b$ and vice versa but does not affect the order in any way, so $f$ will act on $wa$ by changing each $a$ in $w$ to a $b$ and each $b$ in $w$ to an $a$, producing $f(w)$, and then changing that final $a$ to a $b$, producing $f(w)b$. The last three letters of $f(w)$ are $abb$, so the last four letters of $f(wa)$ must be $abbb$. – Brian M. Scott Oct 14 '13 at 01:27
  • 1
    @Dabbish: Thus, it’s just $f(wa)=f(w)b$: process $w$, the front part of the word, in the way that we already know, and then process the new, added letter. And there’s a companion rule for added $b$: $f(wb)=f(w)a$. – Brian M. Scott Oct 14 '13 at 01:31
  • Aha, so that would actuallly be the answer?. By the way again, thanks a lot for your time and help. – Dabbish Oct 14 '13 at 01:32
  • @Dabbish: You’re welcome. I’ve a question for you: how comfortable are you with the concept of proof by induction? I ask because the idea behind these recursive definitions is really very similar. – Brian M. Scott Oct 14 '13 at 01:35
  • Not very comfortable actually. I started learning about recursive and proof by induction yesterday. and now I realize i should try to fully understand proof by induction then probably recursion would be a lot easier I guess? Also do you have any simple advice to proof by induction, what I should think about when it comes to that, definition etc? Thank you – Dabbish Oct 14 '13 at 01:38
  • 1
    @Dabbish: If you were using a text with which I was familiar, I might be able to point you at certain sections, examples, or exercises as being most useful or informative. For now let me point at you at a few questions to which I gave answers that might be useful to you, either as examples or as discussions of the concept: here, here, here, here, and here. – Brian M. Scott Oct 14 '13 at 01:47