1. About type()

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?

new topic     » topic index » view message » categorize

2. Re: About type()

jpartridge said...

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 ' ').

jpartridge said...

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.

jpartridge said...

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

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

3. Re: About type()

  • 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

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

4. Re: About type()

  • 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

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

5. Re: About type()

ghaberek said...

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.

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

6. Re: About type()

_tom said...
  • 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

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

7. Re: About type()

jpartridge said...

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

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

8. Re: About type()

jpartridge said...
... 
            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

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

9. Re: About type()

Mike777b said...
jpartridge said...
... 
            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.

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

10. Re: About type()

jpartridge said...
Mike777b said...
jpartridge said...
... 
            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.

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

11. Re: About type()

  • 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

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

12. Re: About type()

_tom said...

the "and" in the first example does not make sense

That was all I was trying to point out.

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

13. Re: About type()

Mike777b said...
_tom said...

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.

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

14. Re: About type()

_tom said...
  • 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". :)

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

Search



Quick Links

User menu

Not signed in.

Misc Menu