List Info

Thread: "ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer




"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 20:03:04

Hello

I would like to build a boolean function checking whether a given string
represents an integer value :

How to do that with a try and int_of_string ?

the function is :
val is_integer_of_string : string -> bool

Thanks
François Colonna

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 20:16:18

Francois Colonna wrote:
> I would like to build a boolean function checking whether a given string
> represents an integer value :
>
> How to do that with a try and int_of_string ?
>
> the function is :
> val is_integer_of_string : string -> bool

let is_integer_of_string s =
try ignore (int_of_string s); true
with Failure _ -> false

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 20:16:27

On Mon, 18 Dec 2006, Francois Colonna < colonna%40ccr.jussieu.fr">colonnaccr.jussieu.fr> wrote:
&gt;
> How to do that with a try and int_of_string ?
>
> the function is :
> val is_integer_of_string : string -> bool

Is it a homework ? The principle: if [int_of_string] succeeds, one
ignore its returned value and return instead [true], otherwise (in
case an exception occurs) one returns [false]. Just translate that
into code !

ChriS

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 20:14:54

On Mon, 18 Dec 2006, Francois Colonna wrote:

> Hello
>;
> I would like to build a boolean function checking whether a given string
&gt; represents an integer value :
>
&gt; How to do that with a try and int_of_string ?
>
&gt; the function is :
> val is_integer_of_string : string -> bool

let is_i_o_s s =
try let _ = int_of_string s in true
with Failure "int_of_string&quot; -> false

William D. Neumann

---

&quot;There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy.&quot;

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 20:22:26

Christophe TROESTLER wrote:
&gt; Is it a homework ? The principle: if [int_of_string] succeeds, one
> ignore its returned value and return instead [true], otherwise (in
> case an exception occurs) one returns [false]. Just translate that
> into code !

Doh! Francois .. don't look at my email! don't do it!

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] how to build a boolean function checking whether a given string is an integer
user name
2006-12-18 21:09:37

On Mon, 18 Dec 2006, Francois Colonna wrote:

> Hello
>;
> I would like to build a boolean function checking whether a given string
&gt; represents an integer value :
>
&gt; How to do that with a try and int_of_string ?
>
&gt; the function is :
> val is_integer_of_string : string -> bool

I'd say it's a bad design since you would do a lot a of work to just
discard the result. Something like that would be more appropriate:

val as_int : string -> int option

match as_int "123&quot; with
Some n -> ...
| None -> ...

(note that an int is an integer within the range [min_int, max_int], so
you should say "int&quot; even if you don't like abbreviations

Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )