Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 pigLatin - Recursion version
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
java_newbie




PostPosted: Tue Nov 08, 2005 9:37 pm   Post subject: 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

Quote:
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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 08, 2005 9:50 pm   Post subject: (No subject)

Converting a single word to pig latin using recursion is phenomenally easy.

code:
# 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 = <fun>
# word_to_pig_latin "hello";;
- : string = "ellohay"
# word_to_pig_latin "world";;
- : string = "orldway"
# word_to_pig_latin "ooble";;
- : string = "oobleay"
java_newbie




PostPosted: Wed Nov 09, 2005 3:58 pm   Post subject: ooble -> oobleway (o) oobleay (x)

Thanks for your reply, wtd

but there is a problem.

Quote:

# 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




PostPosted: Wed Nov 09, 2005 4:07 pm   Post subject: Re: ooble -> oobleway (o) oobleay (x)

java_newbie wrote:
Thanks for your reply, wtd

but there is a problem.


You're welcome. Smile

java_newbie wrote:
Quote:

# 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.

java_newbie wrote:
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?

java_newbie wrote:
BTW, I writing a program in Java.


Well, you didn't expect me to just give you the answer in Java, did you? Wink

You're gonna have to work for it.
MysticVegeta




PostPosted: Fri Nov 11, 2005 11:59 am   Post subject: (No subject)

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




PostPosted: Fri Nov 11, 2005 2:01 pm   Post subject: (No subject)

MysticVegeta wrote:
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




PostPosted: Sun Nov 13, 2005 2:25 am   Post subject: (No subject)

Yeah i meant loops and conditionals. sorry about that Embarassed
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? Razz
Cervantes




PostPosted: Sun Nov 13, 2005 10:29 am   Post subject: (No subject)

MysticVegeta wrote:
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.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sun Nov 13, 2005 1:07 pm   Post subject: (No subject)

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...
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: