1. add_item question
- Posted by Aveu Sep 24, 2012
- 1618 views
According to the documentation for the add_item procedure (as well as the append procedure which it relies on) the parameter needle is an object and the parameter haystack is a sequence. Going strictly off the documentation I would expect the following to work but it does not:
sequence theList = {} sequence theWord = "test" add_item(theWord,theList,ADD_SORT_UP)
Even enclosing theWord in curlybraces (which should not be necessary) does not help.
Also, there is absolutely no way to tell if the item was added or ignored by the call itself (meaning this procedure really ought to be a function with a results flag returned) meaning this function could really use some kind of function exit status code mechanism.
2. Re: add_item question
- Posted by jimcbrown (admin) Sep 24, 2012
- 1593 views
According to the documentation for the add_item procedure (as well as the append procedure which it relies on) the parameter needle is an object and the parameter haystack is a sequence. Going strictly off the documentation I would expect the following to work but it does not:
sequence theList = {} sequence theWord = "test" add_item(theWord,theList,ADD_SORT_UP)
Even enclosing theWord in curlybraces (which should not be necessary) does not help.
Also, there is absolutely no way to tell if the item was added or ignored by the call itself (meaning this procedure really ought to be a function with a results flag returned)
Where do you see it documented as a procedure? It is clearly a function.
http://openeuphoria.org/docs/std_sequence.html#_2688_add_item
3. Re: add_item question
- Posted by Aveu Sep 24, 2012
- 1572 views
OK, obviously I have had a brain fart, it is a function not a procedure. I have no clue how that got mixed up in my head. But as to the main issue can anyone help ?
4. Re: add_item question
- Posted by jimcbrown (admin) Sep 24, 2012
- 1583 views
OK, obviously I have had a brain fart, it is a function not a procedure. I have no clue how that got mixed up in my head. But as to the main issue can anyone help ?
What is the main issue? What is the problem?
I thought the problem was that theWord was not added to theList, and that I solved it by pointing out that add_item() was a function.
So, based on your example, but fixing your "brain fart", I ran this code:
include std/sequence.e include std/pretty.e sequence theList = {} sequence theWord = "test" theList = add_item(theWord,theList,ADD_SORT_UP) --pretty_print(1, theList) puts(1, theList[1])
and I saw what I expected - theList was set to {"test"}. So, from your point of view, what has gone wrong?
5. Re: add_item question
- Posted by Aveu Sep 24, 2012
- 1570 views
Well, it would appear then that there must be something "subtle" (aka "obvious") wrong with my code that I do not understand. I am an old programmer learning a new language. I ran with trace and things looked good right up to the add_item line but the result was an empty sequence.
As a learning exercise I am writing an anagram solver and the following is my code so far. The relevant variables are Jword1 (aka theWord) and Jwords (aka theList):
include std/console.e include std/io.e include std/os.e include std/pretty.e include std/search.e include std/sequence.e include std/sort.e include std/text.e include std/types.e with batch with trace sequence letters = "txhoefqdueirck" integer size = length( letters ) sequence tumbler = series( 1 , 1 , size ) sequence Jword1 = "" sequence Jwords = {} integer ccflag = 0 function valid_Jword( sequence TUMBLER ) integer ZZ = 0 for XX = 1 to size do ZZ += ( length( find_all ( TUMBLER[XX] , tumbler ) ) - 1 ) end for return ( ZZ = 0 ) end function function make_Jword( sequence TUMBLERZ ) integer ZZ sequence JWORD = repeat( "@" , size ) for XX = 1 to size do ZZ = TUMBLERZ[XX] JWORD[XX] = letters[ZZ] end for return JWORD end function -- initialize -- clear_screen() letters = upper ( sort( letters ) ) tumbler[size] -= 1 -- back off last tumbler by 1 to pre-adjust for the very first pass -- run main process -- loop label "loop_AA" do -- until tumbler[1] > size loop label "loop_BB" do -- until a valid Jword is found ccflag = 1 for CC = size to 1 by -1 label "loop_CC" do tumbler[CC] += ccflag if tumbler[CC] > size then tumbler[CC] = 1 ccflag = 1 else ccflag = 0 end if end for until valid_Jword( tumbler ) -- end loop_BB end loop trace(1) if ( tumbler[1] <= size ) then Jword1 = make_Jword( tumbler ) add_item( Jword1 , Jwords , ADD_SORT_UP ) end if trace(0) until tumbler[1] > size end loop
6. Re: add_item question
- Posted by coconut Sep 24, 2012
- 1561 views
Well, it would appear then that there must be something "subtle" (aka "obvious") wrong with my code that I do not understand. I am an old programmer learning a new language. I ran with trace and things looked good right up to the add_item line but the result was an empty sequence.
As a learning exercise I am writing an anagram solver and the following is my code so far. The relevant variables are Jword1 (aka theWord) and Jwords (aka theList):
include std/console.e include std/io.e include std/os.e include std/pretty.e include std/search.e include std/sequence.e include std/sort.e include std/text.e include std/types.e with batch with trace sequence letters = "txhoefqdueirck" integer size = length( letters ) sequence tumbler = series( 1 , 1 , size ) sequence Jword1 = "" sequence Jwords = {} integer ccflag = 0 function valid_Jword( sequence TUMBLER ) integer ZZ = 0 for XX = 1 to size do ZZ += ( length( find_all ( TUMBLER[XX] , tumbler ) ) - 1 ) end for return ( ZZ = 0 ) end function function make_Jword( sequence TUMBLERZ ) integer ZZ sequence JWORD = repeat( "@" , size ) for XX = 1 to size do ZZ = TUMBLERZ[XX] JWORD[XX] = letters[ZZ] end for return JWORD end function -- initialize -- clear_screen() letters = upper ( sort( letters ) ) tumbler[size] -= 1 -- back off last tumbler by 1 to pre-adjust for the very first pass -- run main process -- loop label "loop_AA" do -- until tumbler[1] > size loop label "loop_BB" do -- until a valid Jword is found ccflag = 1 for CC = size to 1 by -1 label "loop_CC" do tumbler[CC] += ccflag if tumbler[CC] > size then tumbler[CC] = 1 ccflag = 1 else ccflag = 0 end if end for until valid_Jword( tumbler ) -- end loop_BB end loop trace(1) if ( tumbler[1] <= size ) then Jword1 = make_Jword( tumbler ) add_item( Jword1 , Jwords , ADD_SORT_UP ) end if trace(0) until tumbler[1] > size end loop
should be:
Jwords = add_item(Jword1,Jwords,ADD_SORT_UP)
7. Re: add_item question
- Posted by Aveu Sep 24, 2012
- 1626 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
8. Re: add_item question
- Posted by EUWX Sep 24, 2012
- 1554 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
In Euphoria,
A procedure DOES NOT RETURN a result
A function DOES RETURN a result
Therefore
If you are calling a function you MUST MUST MUST "catch" the result in a pre-defined variable of the type of the expected result.
That is what everybody is trying to tell you.
9. Re: add_item question
- Posted by euphoric (admin) Sep 24, 2012
- 1540 views
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error.
That is correct.
Is that in the documentation ?
The only reference I could find is in 1.3.3 Language Enhancements: "Function results can now be ignored."
There should at least be a comment about it in 4.2.1.2. functions.
10. Re: add_item question
- Posted by evanmars Sep 25, 2012
- 1470 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
In Euphoria,
A procedure DOES NOT RETURN a result
A function DOES RETURN a result
Therefore
If you are calling a function you MUST MUST MUST "catch" the result in a pre-defined variable of the type of the expected result.
That is what everybody is trying to tell you.
No, you do not need to "catch" the result anymore, as CK stated.
My question is, is there really any need for
procedure
any more? Other than saving on typing, no need for
return 0
?
11. Re: add_item question
- Posted by jimcbrown (admin) Sep 25, 2012
- 1464 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
In Euphoria,
A procedure DOES NOT RETURN a result
A function DOES RETURN a result
Therefore
If you are calling a function you MUST MUST MUST "catch" the result in a pre-defined variable of the type of the expected result.
That is what everybody is trying to tell you.
No, you do not need to "catch" the result anymore, as CK stated.
In general, true. In the specific case of add_item(), you still do. Calling add_item() but not using the return value is pointless, as add_item() has no side effects.
My question is, is there really any need for
procedure
any more? Other than saving on typing, no need for
return 0
?
Well, no not really. Getting rid of it would break a lot of code though.
12. Re: add_item question
- Posted by evanmars Sep 25, 2012
- 1456 views
Not really asking to get rid of it. Just wondering if I wasn't seeing the whole picture (very possible).
13. Re: add_item question
- Posted by ne1uno Sep 25, 2012
- 1529 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
In Euphoria,
A procedure DOES NOT RETURN a result
A function DOES RETURN a result
Therefore
If you are calling a function you MUST MUST MUST "catch" the result in a pre-defined variable of the type of the expected result.
That is what everybody is trying to tell you.
No, you do not need to "catch" the result anymore, as CK stated.
My question is, is there really any need for
procedure
any more? Other than saving on typing, no need for
return 0
?
maybe there should be an error when you don't catch the result from a function that the only reason you are calling it is to get the result?
euphoria can't change the sequence from the function call alone. you must catch the result.
14. Re: add_item question
- Posted by mattlewis (admin) Sep 25, 2012
- 1521 views
maybe there should be an error when you don't catch assign the result from a function that the only reason you are calling it is to get the result?
euphoria can't change the sequence from the function call alone. you must catch assign the result.
"Catch" is the wrong term for this. That's commonly used for exceptions. What we're talking about here is an assignment.
Possibly a warning would be more appropriate (didn't we previously talk about doing this?). I'm not sure I'd want that to be an error. Have to think about it. We do attempt to track which routines have side effects.
Matt
15. Re: add_item question
- Posted by EUWX Sep 25, 2012
- 1481 views
DOH! Yep, obvious!
OK, so what I learned is that a FUNCTION in Euphoria can be called like a PROCEDURE (not located to the right of an '=' character) without generating an error. Is that in the documentation ?
In Euphoria,
A procedure DOES NOT RETURN a result
A function DOES RETURN a result
Therefore
If you are calling a function you MUST MUST MUST "catch" the result in a pre-defined variable of the type of the expected result.
That is what everybody is trying to tell you.
No, you do not need to "catch" the result anymore, as CK stated.
My question is, is there really any need for
procedure
any more? Other than saving on typing, no need for
return 0
?
maybe there should be an error when you don't catch the result from a function that the only reason you are calling it is to get the result?
euphoria can't change the sequence from the function call alone. you must catch the result.
I agree that the word "catch" is not appropriate in high level discussion of a basic type of language; I should have said "assignment".
The best approach (and I can't think of any language that has it all) would be to have ALL functions/procedures return two results
(which means, of course, that procedures and functions are no different from each other.)
One would be a sequence contain information on error code, location of error etc. This would ALWAYs be available upon exiting from function in a Global variable 'lastcallerror". The other would be the main returned result.
A system variable which is programmer/user settable would allow an OnError module to selectively take over for
1. unpardonable (un-proceedable) error;
2. slightly tolerable error;
3. moderately tolerable level;
4. Warning level error.
Thus, even if an error occurs say at the 200th element (e.g. divide by zero) there would be 199 properly done elements of the new sequence available as argument to the next function SHOULD the programmer want to use them.
Matt et all. It is not difficult at all to change the Euphoria procedures to start returning a null, which the old programs are already ignoring, and the new programs would know what the result is going to be.
16. Re: add_item question
- Posted by coconut Sep 25, 2012
- 1476 views
maybe there should be an error when you don't catch assign the result from a function that the only reason you are calling it is to get the result?
euphoria can't change the sequence from the function call alone. you must catch assign the result.
"Catch" is the wrong term for this. That's commonly used for exceptions. What we're talking about here is an assignment.
Possibly a warning would be more appropriate (didn't we previously talk about doing this?). I'm not sure I'd want that to be an error. Have to think about it. We do attempt to track which routines have side effects.
Matt
definitely it should be a warning not an error.
Jacques