
-----------------------------------
java_newbie
Tue Nov 08, 2005 9:37 pm

pigLatin - Recursion version
-----------------------------------
Hi, guys

I am currently working on converting English sentence into pig latin using

recursion. To generate some ideas, I went to wikipedia, and it said

It is also a common programming exercise to define a procedure or function that, when inputted a word in normal English, outputs the Pig Latin equivalent. It is often used to teach the concept of recursion. The simplest way to write such a procedure is to set a base case for words beginning with vowels (add 'ay), and a recursive case that moves the first letter to the end of the word for reevaluation.

However, in my case, I have to do the followings:

if the first letter is a vowel then word+"way"

if the first letter is not a vowel then ordw+"ay"

There is no problem doing it in a "normal" way, but it is terribly hard to accomplish such a task using recursion.

Can you give me at least an idea of how to approach to the solution to this problem?

-----------------------------------
wtd
Tue Nov 08, 2005 9:50 pm


-----------------------------------
Converting a single word to pig latin using recursion is phenomenally easy.

# let rec word_to_pig_latin word =
    let is_vowel letter =
      let letter = Char.lowercase letter in
        letter = 'a' or letter = 'e' or letter = 'i'
        or letter = 'o' or letter = 'u'
    in
      if word = "" then
        ""
      else if is_vowel word.[0] then
        word ^ "ay"
      else
        let first_char = word.[0]
        and rest = String.sub word 1 (String.length word - 1)
        in
          word_to_pig_latin (rest ^ Char.escaped first_char);;
val word_to_pig_latin : string -> string = 
# word_to_pig_latin "hello";;
- : string = "ellohay"
# word_to_pig_latin "world";;
- : string = "orldway"
# word_to_pig_latin "ooble";;
- : string = "oobleay"

-----------------------------------
java_newbie
Wed Nov 09, 2005 3:58 pm

ooble -&gt; oobleway (o) oobleay (x)
-----------------------------------
Thanks for your reply, wtd

but there is a problem.


# word_to_pig_latin "ooble";; 
- : string = "oobleay"            (it should be "oobleway")


As it has already mentioned, if the first letter of the word is vowel, 

then add "way" not "ay".

Also, converting a word is relatively easy, but converting a sentence 

using recursion is awfully complicated.

BTW, I writing a program in Java.

-----------------------------------
wtd
Wed Nov 09, 2005 4:07 pm

Re: ooble -&gt; oobleway (o) oobleay (x)
-----------------------------------
Thanks for your reply, wtd

but there is a problem.

You're welcome.  :)


# word_to_pig_latin "ooble";; 
- : string = "oobleay"            (it should be "oobleway")


As it has already mentioned, if the first letter of the word is vowel, 

then add "way" not "ay".

That's easy enough to fix.

Also, converting a word is relatively easy, but converting a sentence 

using recursion is awfully complicated.

Not especially.  When you think about this problem, break it down into a series of smaller problems.  Can you take a string and break it into words?

BTW, I writing a program in Java.

Well, you didn't expect me to just give you the answer in Java, did you?  ;)

You're gonna have to work for it.

-----------------------------------
MysticVegeta
Fri Nov 11, 2005 11:59 am


-----------------------------------
This problem is a horrible way of making learn recursion, dont you agree wtd? because when it can be achieved using "if" structures, this is redundant. I think a grid/maze problem would have  been better.

-----------------------------------
wtd
Fri Nov 11, 2005 2:01 pm


-----------------------------------
This problem is a horrible way of making learn recursion, dont you agree wtd? because when it can be achieved using "if" structures, this is redundant. I think a grid/maze problem would have  been better.

I hope you meant loops, because recursion and "if" are not mutually exclusive.

-----------------------------------
MysticVegeta
Sun Nov 13, 2005 2:25 am


-----------------------------------
Yeah i meant loops and conditionals. sorry about that  :oops: 
I find the grid problems sort of addictive, when you learn recursion and solve a grid problem using it, its just irresistable to use that method again to solve other maze/path/grid problems, dont you think? :P

-----------------------------------
Cervantes
Sun Nov 13, 2005 10:29 am


-----------------------------------
This problem is a horrible way of making learn recursion, dont you agree wtd? because when it can be achieved using "if" structures, this is redundant. I think a grid/maze problem would have  been better.
Recursion and grids are fun, but it is quite different.  For that, you need to use recursion.  For something like this, it is not necessary, though it is a valid solution.  Teaching recursion with this not only teaches the concept of recursion, but also illustrates that recursion can be used in place of loops, an idea I am coming to like as I spend more time with O'Caml.

-----------------------------------
md
Sun Nov 13, 2005 1:07 pm


-----------------------------------
The best thing I've found to teach recursion is mine sweeper. It's very simple to explain how the reveal works, and it's ideally suited to recusion as the recursive method is so much nicer then the non-recursive method.

I dunno what dialect of pig latin they're teaching now-a-days, but it used to be that you took all the consanents from the front of a word (in a block) and moved it to the end of the word, and added "ay". That's definitely not how you're doing it...
