
-----------------------------------
Thuged_Out_G
Sat Apr 29, 2006 12:01 am

Vowel/Constanent
-----------------------------------
Was bored, and saw another user was asking how to do this .. so i decided to add it to turing. So it can be easily used.

This module will check a given letter if it is a vowel/consonant. 


Check.tu

unit
module Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        if index (w, "a") not= 0 or index (w, "e") not= 0 or index (w, "i") not= 0 or index (w, "o") not= 0 or index (w, "u") not= 0 or index (w, "A") not= 0 or index (w, "E") not= 0 or index (w, "I") not= 0 or index (w, "O") not= 0 or index (w, "U") not= 0 then
            result true
        else
            result false
        end if
    end Vowel

    fcn Cons (w : string) : boolean
        if index (w, "a") not= 0 or index (w, "e") not= 0 or index (w, "i") not= 0 or index (w, "o") not= 0 or index (w, "u") not= 0 or index (w, "A") not= 0 or index (w, "E") not= 0 or index (w, "I") not= 0 or index (w, "O") not= 0 or index (w, "U") not= 0 then
            result false
        else
            result true
        end if
    end Cons
end Check


Add that to "%oot/support/predefs", save as "check.tu"
Then add the line "%oot/support/predefs/Check.tu" to "%oot/support/predefs/predefs.lst" right at the end of the rest of the functions/procuderes

you can then call it by


put Check.Vowel("E") %returns true
put Check.Vowel("B") %returns false
put Check.Cons("B") %returns true
put Check.Cons("E") %returns false


May be useful in some programs, im sure just about every compsci student in HS has had an assignment where that could have been useful at some point. :P

-----------------------------------
Thuged_Out_G
Sat Apr 29, 2006 12:11 am


-----------------------------------
Wow, i spelt consonant wrong. In both the thread name and fcn  :oops:

Also, is there a way for (w:string) to be a variable number of arguments.
Like i think in ruby it is "*args". Is there anything like this in turing.

-----------------------------------
Cervantes
Sat Apr 29, 2006 6:39 am


-----------------------------------
Why not just add this to the String module?


Also, is there a way for (w:string) to be a variable number of arguments.
Like i think in ruby it is "*args". Is there anything like this in turing.

That's right. *args in ruby, however, is basically an array.

irb(main):001:0> def foo(*args)
irb(main):002:1>   p *args
irb(main):003:1>   p args
irb(main):004:1> end
=> nil
irb(main):005:0> foo(1, 2, 3)
1
2
3
[1, 2, 3]
=> nil

You can do a similar thing in Turing:

proc foo (args : array 1 .. * of int)

However, all the arguments must be of the same type, and you'll have to make an array before hand to send to foo. Lots of extra work.

Also, it would be easier if you converted the word to all upper case or all lower case so you didn't have to check "e" and "E".

-----------------------------------
do_pete
Sat Apr 29, 2006 9:50 am


-----------------------------------
You only really need the Vowel fcn because if it returns false then you know its a consonant

-----------------------------------
Delos
Sat Apr 29, 2006 11:58 am


-----------------------------------
You only really need the Vowel fcn because if it returns false then you know its a consonant

Quite true.  If you *really* want to have both Vowel and Cons fcns, simply create a 'Check' fcn, and call that fcn from within the Vowel and Cons structures.  The Check fcn would essentially do what is already being done.

-----------------------------------
Thuged_Out_G
Sat Apr 29, 2006 12:30 pm


-----------------------------------
This sounds n00bish, but how do you change a letter to upper or lowercase?
Also, is there another way to do this? Other than using 'index'. I know i could use ord, IS there another option?

-----------------------------------
[Gandalf]
Sat Apr 29, 2006 12:36 pm


-----------------------------------
Either use Str.Upper() and Str.Lower() or if you want to create the functions yourself read Cervantes' string manipulation tutorial which is found in the Turing Walkthrough.

-----------------------------------
TokenHerbz
Sat Apr 29, 2006 12:46 pm


-----------------------------------

for i: 65 .. 90
    put chr(i) ..
    put chr(i + 32) ..
end for


you can get the number of the letter with ord(letter)

EDIT:  gandalf, my turing dosn't have "Str.Upper() and Str.Lower()"

-----------------------------------
wtd
Sat Apr 29, 2006 1:06 pm


-----------------------------------
With a little work this code can be much more concise.

untested

unit
module Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        var w2 : string = Str.Lower (w)
        var vowels : string = "aeiou"
        
        for i : 1..5
            if index (vowels (i), w2) not= 0 then
               result true
            end if
        end for
        
        result false
    end Vowel

    fcn Cons (w : string) : boolean
        var w2 : string = Str.Lower (w)
        var o : int = ord (w2)
    
        result (o >= 65 and o  0 then
         return true
else
     return false

note: this is untested too

Ofcourse your way would work too, by cehcking each letter but one thing I learned, why use redundant code?

Note: I dont know what happened, why did it double post?? Could someone delete it please, i really dont know what happened, sorry guys

-----------------------------------
[Gandalf]
Tue May 02, 2006 1:18 am


-----------------------------------
I would guess that wtd programs in Turing less than regularily, and he probably doesn't even have it installed, so any mistakes can hopefully be excused.

Anyway, ironically, you are wrong and wtd had it right.  The way you had it the program would check if string contained every vowel in order.  You should be checking to see if w2 contains any of the letters in vowels.  I hope that by now you know that in Turing you access the character at a specified index in a string the same way you access the element at a specified index in an array.  
Also, you got the arguments to the index function wrong, it's:
index (string, vowels)

Now, some minor syntatic troubles in wtd's code:
module Check
    import Str   %imported the Str module explicitly for use in Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        var w2 : string := Str.Lower (w)   %here and in a few other places the = was turned to Turing's :=
        var vowels : string := "aeiou"
       
        for i : 1..5
            if index (w2, vowels (i)) not= 0 then   %fixed syntax of index()
               result true
            end if
        end for
       
        result false
    end Vowel

    fcn Cons (w : string) : boolean
        var w2 : string := Str.Lower (w)
        var o : int := ord (w2)
   
        result (o >= 65 and o = 65 and ord (w)  122 or ord (w1) >= 91 and ord (w1) 