1. type fruit(sequence unknown_fruit)

From A Beginners Guide to Euphoria:

type fruit(sequence unknown_fruit)
sequence valid_fruit
valid_fruit = {"bananas", "apples", "oranges", "pears"}
return find(unknown_fruit, valid_fruit)
end type


This is wierd to me.
It would make more sense like this:

fruit_check = "pears"
fruit fruit_check
puts(1, "Program Run Completed\n")
or
fruit_check = "pears"
fruit_check = fruit(fruit_check)
puts(1, "Program Run Completed\n")


Please, Why is it structured like this?
Thanks!

new topic     » topic index » view message » categorize

2. Re: type fruit(sequence unknown_fruit)

vmars said...

From A Beginners Guide to Euphoria:

type fruit(sequence unknown_fruit)
sequence valid_fruit
valid_fruit = {"bananas", "apples", "oranges", "pears"}
return find(unknown_fruit, valid_fruit)
end type

This defines the fruit type so you can use it later

said...



This is wierd to me.
It would make more sense like this:

fruit_check = "pears"
fruit fruit_check
puts(1, "Program Run Completed\n")
or
fruit_check = "pears"
fruit_check = fruit(fruit_check)
puts(1, "Program Run Completed\n")


Please, Why is it structured like this?
Thanks!

using it later

fruit fruit_check                --defining a variable of type fruit 
 
if fruit("pears") then           --test of pears is a fruit 
    puts(1, "Valid fruit!\n") 
    fruit_check ="pears"         --fruit check can now be "pears" 
else  
    puts(1, "Not a valid fruit!\n") 
    fruit_check = "pears"        --fruit_check can't be "pears" - will crash the program 
                                 --add pears to the list of valid fruits 
end if 
 
 

Chris

new topic     » goto parent     » topic index » view message » categorize

3. Re: type fruit(sequence unknown_fruit)

Hi

I wish I could edit my posts.

Changes pears in what I wrote to grapes, then you can crash the program!

Chris

new topic     » goto parent     » topic index » view message » categorize

4. Re: type fruit(sequence unknown_fruit)

ChrisB said...

I wish I could edit my posts.

Or maybe there could be a forced preview prior to posting.

new topic     » goto parent     » topic index » view message » categorize

5. Re: type fruit(sequence unknown_fruit)

euphoric said...
ChrisB said...

I wish I could edit my posts.

Or maybe there could be a forced preview prior to posting.

Ouch. In this case, I did, but I'm just too stupid/lazy at times to be bothered to properly check at times. Just like 90% of my programming time is spent adding the trailing )

Chris

new topic     » goto parent     » topic index » view message » categorize

6. Re: type fruit(sequence unknown_fruit)

ChrisB said...
euphoric said...
ChrisB said...

I wish I could edit my posts.

Or maybe there could be a forced preview prior to posting.

Ouch.

That wasn't meant to cause pain! grin I'd like a forced preview feature for myself! smile

new topic     » goto parent     » topic index » view message » categorize

7. Re: type fruit(sequence unknown_fruit)

euphoric said...
ChrisB said...
euphoric said...
ChrisB said...

I wish I could edit my posts.

Or maybe there could be a forced preview prior to posting.

Ouch.

That wasn't meant to cause pain! grin I'd like a forced preview feature for myself! smile

I alwyas perveiw.

Matt

new topic     » goto parent     » topic index » view message » categorize

8. Re: type fruit(sequence unknown_fruit)

Do you mean like this?:

fruit fruit_check                --defining a variable of type fruit  
  
if fruit("grapes") then           --test of grapes is a fruit  
    puts(1, "Valid fruit!\n")  
    fruit_check ="grapes"         --fruit check can now be "grapes"  
else   
    puts(1, "Not a valid fruit, so added to list!\n")  
    fruit_check = "grapes"        --fruit_check can't be "pears" - will crash the program  
                                 --add grapes to the list of valid fruits  
end if  



fruit_check = "grapes" fruit_check can't be "pears" - will crash the program
Why, because "pears" is already there?
If so, What if I wanted two "pears" entries?
Thanks!

new topic     » goto parent     » topic index » view message » categorize

9. Re: type fruit(sequence unknown_fruit)

vmars said...

Do you mean like this?:

fruit fruit_check                --defining a variable of type fruit  
  
if fruit("grapes") then           --test of grapes is a fruit  
    puts(1, "Valid fruit!\n")  
    fruit_check ="grapes"         --fruit check can now be "grapes"  
else   
    puts(1, "Not a valid fruit, so added to list!\n")  
    fruit_check = "grapes"        --fruit_check can't be "pears" - will crash the program  
                                 --add grapes to the list of valid fruits  
end if  



fruit_check = "grapes" fruit_check can't be "pears" - will crash the program
Why, because "pears" is already there?
If so, What if I wanted two "pears" entries?
Thanks!

Hi

Yes, thats what I meant. Of you could have as many pears as you wanted, but the find would return true no matter how many pears you had. Of course, that would always be beaten by a full house (sorry, couldn't resist)

The program was meant to be demonstration of checking the type - you can't add the wrong type to a typed variable, but you can use the type as a function to check if it is the right type.

Sorry I botched it up.

As far as I know, a type is defined at run time, so you can't add another element or range to a type after it is defined. However you could create a function that checks for validity then adds it to a list of valid elements.

Chris

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu