string->integer STRING START END -> INTEGER or #f500 Can't connect to 127.0.0.1:8778 (connect: Connection refused) Here
incis a macro or a function that returns the incremented argument. On many Scheme systems, it can be implemented more efficiently than merely
(+ 1 x)if we assume that
xis a fixnum.
END(exclusive) is a representation of a non-negative integer in decimal notation. If this is the case, this integer is returned. Otherwise -- when the substring contains non-decimal characters, or when the range from
ENDis not within
STRING, the result is
#f. This procedure is a specialization of the standard
string->number. The latter is far more general: for example, it will try to read strings like "1/2", "1S2", "1.34" and even "1/0" (the latter causing a zero-divide error). Note that to
string->number, "1S2" is a valid representation of an inexact integer 100. Oftentimes we want to be more restrictive about what we consider a number: we want merely to read an integral label. The obvious method is to use R5RS
string->numberas a test.
string->numbercan convert strings containing base 2, 8, 10, and 16 numbers into a number. You can also force a conversion by formatting the number using Scheme conventions for specifying bases. If the conversion can't be performed,
string->numberreturns #f: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused) This is only half the solution, however.
string->numberworks for any kind of number, so technically, you should wrap the call in a call to the
integer?predicate: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused) However, the simple solution has some notable drawbacks. It must be noted that using
string->numberfor testing if a string represents an integer has notable drawbacks. Scheme's concept of number is a lot broader than most languages', so
string->numberis much more general than your typical programming language. Scheme understands arbitrarily large numbers like 2 ^ 80, fractions such as 1/2, and imaginary numbers: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused) Scheme's number facilities can lead to surprises. For example, on Petite Chez Scheme and Gambit, 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused) Not too many people would take "1S0" to mean an integer. It means an inexact integer. There is even more serious problem. Often we test if a string represents an integer when validating user input. It is highly preferable if the test is a total predicate, that is, generates no errors. However,
(string->number "1/0")will raise a run-time error. -- OlegK - 14 Sep 2004 (corrected solution) -- GordonWeakliem - 23 Apr 2004 (simple solution) http://pobox.com/~oleg/ftp/Scheme/util.html#misc-str-util NoelWelsh - 14 Sep 2004 I moved STOPINCLUDE; thank you for clarifying it. I thought about how to merge two solutions, and the best I have come with is to add one level of headers, and make a forward reference. Any hints how to do the merging better? Both solutions will work, depending on circumstances -- OlegK - 14 Sep 2004 My preference is to give the correct solution (i.e. Oleg's) under solution and put the rest under discussion. In this case, I put the discussion of the correct solution first and then went into a discussion of Scheme's number system, which leads into why
string->numberis insufficient. Also, the definition of
incseems worthy of a recipe of its own under NumberRecipes, I think. -- GordonWeakliem - 14 Sep 2004 Note that
add1in MzScheme. In my opinion the portable solution
(+ x 1)is in the context of a cookbook better (easier to grasp). Besides, most compilers ought to recognize this pattern anyway. -- JensAxelSoegaard - 14 Sep 2004