Re: String?
- Posted by Nicholas Koceja <Nickofurr at aol.com> May 31, 2004
- 690 views
Juergen Luethje wrote: > > Rolf wrote: > > > Hi 'string fans'! > >> > > As I know, a character is a byte that represents a human readable or > > printable symbol. A character string (synonymous: string) is a series of > > characters. i.e., a series of bytes representing human readable|printable > > symbols (words, sentences,...). > > Again: I never heard or read, that the definition of "character" or > "string" depends on the question, whether or not something is printable. > E.g. in BASIC, this is clearly *not* the case. You might also want to > look here: > <a > href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?characters">http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?characters</a> > <a > href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?string">http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?string</a> > > Of course, under certain circumstances, it is useful to know, whether or > not a given string is printable, but it has nothing got to do with its > *definition*. > > > Is it important to differentiate between a general byte series (#00 to #FF) > > and a 'string'? > > No, this is the same. > > > 1) If there are 256 readable|printable symbols assigned to the > > numbers #00 to #FF, then it's impossible do decide, if you have a > > 'string' or not! > > > > 2) If you declare at least one byte not to be a readable|printable > > symbol, then you may declare any byte series of this type as a 'string' > > in > > comparison to a generally byte series, which may contain any byte between > > #00 and #FF. In C, i.e., #00 is assumed to be such a byte, and therefore > > a byte series ending with the byte #00 is declared as such a type of > > string (Null terminated string). This makes sense only for specially > > written 'string handling routines' (stringcmp(), printf(),...), nothing > > else. > > > > 3) For I know what I would like to read|write|print, Euphoria gives you the > > opportunity to decide, what you would like to handle as a 'string' or > > not. > > In practice I don't see any necessity to have a so called string type, it > > makes no real sense. However, if you believe you need it, then use a > > type function similar like that, what Nicholas Koceja has given as an > > example. > > Like him, you are missing the point. > Using such a user-defined string type doesn't solve the problem: If a > Euphoria program reads e.g. {74,111,104,110} from a file, there is no way > to find out, whether this sequence means "John", or the weight of the > members of my family, or whatever. First off, Why shuold the program differenciate from these two? take this example: The "key.ex" program.
-- find out what numeric key code is generated by any key on the keyboard -- usage: -- ex key integer code puts(1, "Press any key. I'll show you the key code. Press q to quit\n\n") while 1 do code = get_key() if code != -1 then <b> printf(1, "The key code is: %d\n", code)</b> if code = 'q' then exit end if end if end while
In the program, it thinks of code as an 'integer'. With a string typpe, you could NOT have code as integer, it would have to be OBJECT, since a Chaacter could be aan integer, or a String. the key statement is in bold. If there was a String type, this statement would cause an error. Furthermore, if you defined double-quotes as a "String", then the '%d' would cause the error, since a "string" can not have a decimal value. If you make it so that a "string" can have one, you would be destroying what you tried to prove in the first place. Take a look at this:
sequence string, seq atom at string = "" seq = {} procedure same(object a) if equal(string, a) then puts(1, "TRUE\n\n") else puts(1, "FALSE\n\n") end if end procedure puts(1, "\"\" = {}\n") same(seq) puts(1, "\"abcd\" = {\'a\',\'b\',\'c\',\'d\'}\n") string = "abcd" seq = {'a','b','c','d'} same(seq) puts(1, "\"a\" = \'a\'\n") string = "a" at = 'a' same(at) while get_key() = -1 do end while
This may make you think that there is no differenciation. However, the program will output the following, then wait for a key to be pressed before exiting: "" = {} TRUE "abcd" = {'a','b','c','d'} TRUE "a" = 'a' FALSE An atom, 'a', is not possible if quotes were strings. To tell you the honest truth, a "string" type would be pointless, and would make Euphoria a tad slower. If you were to include a string type, you would have to change how Euphoria looks at sequences, and "strings", for that matter. As you all know, Euphoria is a fast, and easy to understand (Not always easy to learn) Programming language. As it states in "ed.doc", Euphoria uses pure numbers. The smallet unit is an "atom". Atoms and Sequences are the ony two explicitly defined types. An Integer is a type of atom, and an object can be a sequence, as well as an atom. What I think we need to find out, is this Question: "What exactly is a String?" If a string is just defined as in BASIC: "A string of letters or numnbers." then yes, we could benifit from it. However, if a string is defined as a sequence of characters, then ANY seqence of positive integers would fit the description. Although the character page only goes to 255, you can "print" ANY positive integer. Try it yourself. Therefdore, the question of wether a string type should be included or not all depends on the answer to the question: "What exactly defines a String?" > Surely any string has to be a sequence of special integers (which can be > checked by such a user-defined type), but not any such sequence is a > string! > > Regards, > Juergen > > | Programs Incomplete: | 20-30 | | Operating System: | Windows XP |