1. About type()
- Posted by jpartridge May 30, 2018
- 1435 views
I've been going through Stephen C. Baxter's book How To Program Computers. The book is good but the printed code examples are full of typos and other mistakes. Until now I've managed to correct the mistakes with the help of the debugger, but yesterday I came to something I don't understand. It's about the type routine, as he calls it. The code in the example is as follow:
type string(sequence st) integer ln ln = length(st) for i = 1 to ln do if integer(st[i]) then --ASCII code range if st[i] < 0 and st[i] > 255 then --if one fails, the whole seq fails return 0 end if else --if one fails, the whole seq fails return 0 end if end for --if you got here, success return 1 end type --Now you can declare variables of the string type string fname, lname
This seemed quite cool and was obviously asking to be tested... so I added:
string flname fname = "Awe" lname = "some!\n" flname = fname & lname printf(1, "%s", {flname})
and the output was, as expected... Awesome! So far, so good. However, the example on the book goes on with the following script:
-- you can also test a variable for string credentials if string(seq4) then seq4[1] = 32 else puts(1, "Not a string.\n") end if
And this I couldn't understand. What's this supposed to do?
But my main question about this subject is (as I work a lot with strings): Can this type() things be added to the built-in functions? Thank you.
P.S.: This Stephen C. Baxter is not the famous science fiction author, is it?
2. Re: About type()
- Posted by ghaberek (admin) May 30, 2018
- 1376 views
And this I couldn't understand. What's this supposed to do?
It checks to see if seq4 is a string, and if it is, changes the first character to a space (ASCII character 32, or ' ').
Can this type() things be added to the built-in functions?
Euphoria has only four built-in types: atom, sequence, object, and integer. But there are several more available via the extended types library, including string and boolean.
P.S.: This Stephen C. Baxter is not the famous science fiction author, is it?
I wouldn't think so, but stranger things have happened.
-Greg
3. Re: About type()
- Posted by _tom (admin) May 30, 2018
- 1333 views
- What is the source of this book?
- OE thinking is truly about thinking in just atom and sequence
- types are there as a sanity check, after a program runs people add without typecheck to get the program to run a bit faster
- Phix has a string datatype, which is a sequence with storage optimized for utf8 characters -- but is a sequence otherwise
- Euphoria types should not be confused with types found in conventional languages
- Euphoria atom|sequence eliminates a lot of the pain associated with conventional datatypes
_tom
4. Re: About type()
- Posted by _tom (admin) May 30, 2018
- 1382 views
- The first example just tests if a sequence could be plain text. But most text is now utf8. And, some will say utf32 is more useful. (Windows formats, of course, should be banned.)
- You can write a procedure or function using a user defined type. Use them in body code, or as parameters.
_tom
5. Re: About type()
- Posted by jpartridge May 31, 2018
- 1336 views
Euphoria has only four built-in types: atom, sequence, object, and integer. But there are several more available via the extended types library, including string and boolean.
Yes, of course! The extended types library... Why didn't I remember to check that? ;)
Now, seriously, thanks.
6. Re: About type()
- Posted by jpartridge May 31, 2018
- 1322 views
- What is the source of this book?
I don't exactly remember where I got it but I can assure you it has a respectable provenience. :)
I suppose it was downloaded from one of the sites you recommend. - perhaps usingeuphoria.com
7. Re: About type()
- Posted by ghaberek (admin) May 31, 2018
- 1353 views
I suppose it was downloaded from one of the sites you recommend. - perhaps usingeuphoria.com
Yes, there's a copy in The Archive and in my copy on usingEuphoria.com. It's filed under Documentation.
Here is a direct link: https://archive.usingeuphoria.com/how2prog.pdf
-Greg
8. Re: About type()
- Posted by Mike777b May 31, 2018
- 1326 views
... if st[i] < 0 and st[i] > 255 then --if one fails, the whole seq fails return 0 end if ...
Won't the first line always fail?
mike
9. Re: About type()
- Posted by jpartridge Jun 01, 2018
- 1234 views
... if st[i] < 0 and st[i] > 255 then --if one fails, the whole seq fails return 0 end if ...
Won't the first line always fail?
mike
Nope.
10. Re: About type()
- Posted by Mike777b Jun 01, 2018
- 1244 views
... if st[i] < 0 and st[i] > 255 then --if one fails, the whole seq fails return 0 end if ...
Won't the first line always fail?
mike
Nope.
OK, I'll bite. Give me a value for i which will result in an immediate return with value 0.
11. Re: About type()
- Posted by _tom (admin) Jun 01, 2018
- 1281 views
- a simple view of a valid number for a character is 0 to 255
... if st[i] < 0 or st[i] > 255 then return 0 end if ...
the "and" in the first example does not make sense
- ascii is 0 to 127
(I am excluding the historical extended ascii values)
- looks like utf8 has a few tricks to it
nominally the range is 0 to 255
But, looking at https://en.wikipedia.org/wiki/UTF-8 they describe "red" values
192 to 193 245 to 255
A better type definition is then:
-- permitted values for ascii text -- 0 to 127 -- utf8 text -- "red" values 192-193 245-255 -- ref: https://en.wikipedia.org/wiki/UTF-8 type string( sequence str ) for i=1 to length(str) do atom chr = str[i] if not integer(chr) then return 0 end if if chr<0 or chr>244 then return 0 end if if chr=192 or chr=193 then return 0 end if end for return 1 end type
_tom
12. Re: About type()
- Posted by Mike777b Jun 02, 2018
- 1177 views
the "and" in the first example does not make sense
That was all I was trying to point out.
13. Re: About type()
- Posted by jpartridge Jun 02, 2018
- 1157 views
the "and" in the first example does not make sense
That was all I was trying to point out.
You're right. I'll change the nope to a yup. :)
This, of course, only proves what I said about the poor printed code examples on the mentioned book.
14. Re: About type()
- Posted by jpartridge Jun 02, 2018
- 1197 views
- a simple view of a valid number for a character is 0 to 255
... if st[i] < 0 or st[i] > 255 then return 0 end if ...
the "and" in the first example does not make sense
- ascii is 0 to 127
(I am excluding the historical extended ascii values)
- looks like utf8 has a few tricks to it
nominally the range is 0 to 255
But, looking at https://en.wikipedia.org/wiki/UTF-8 they describe "red" values
192 to 193 245 to 255
A better type definition is then:
-- permitted values for ascii text -- 0 to 127 -- utf8 text -- "red" values 192-193 245-255 -- ref: https://en.wikipedia.org/wiki/UTF-8 type string( sequence str ) for i=1 to length(str) do atom chr = str[i] if not integer(chr) then return 0 end if if chr<0 or chr>244 then return 0 end if if chr=192 or chr=193 then return 0 end if end for return 1 end type
_tom
As an old professor used to say, "here's a very crisp piece of code". :)