String manipulation.

Chris Angus CAngus@Armature.com
Thu, 7 Feb 2002 18:11:15 -0000


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C1B002.D5619590
Content-Type: text/plain;
	charset="iso-8859-1"

You may want to use reads
as read will call error if the 
string is not an integer

or use something akin to the example below

readMaybeInt :: String -> Maybe Int
readMaybeInt s = case reads s of
                      [(x,_)] -> Just x
                      _       -> Nothing



-----Original Message-----
From: Hal Daume III [mailto:hdaume@ISI.EDU]
Sent: 07 February 2002 17:54
To: DK
Cc: haskell@haskell.org
Subject: Re: String manipulation.


You should have a look at the Read class.  A member a of the Read class
has a function read :: String -> a.

so, for example, you can say:

  (read "5.0") :: Double

to read this as a double.  You could also read it as an integer or
something.

If you have a list of strings that you want to convert to a list of
integers, you could do:

  map (\x -> (read x)::Double) [list of strings]

Hope that helps.

 - Hal

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

On Thu, 7 Feb 2002, DK wrote:

> Hello. First of all I am a beginner in Haskell, and I must confess I do
not yet fully understand it. I need to write a program in Haskell, though,
and I am having some difficulties... 
> 
> What I would like to ask, is how can I take a string from a list, and
manipulate it, in order to convert it to an integer. Any help would be very
appreciated.
> 
> Thank you very much,
>     Dimitris
> 

_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell

------_=_NextPart_001_01C1B002.D5619590
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>RE: String manipulation.</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>You may want to use reads</FONT>
<BR><FONT SIZE=3D2>as read will call error if the </FONT>
<BR><FONT SIZE=3D2>string is not an integer</FONT>
</P>

<P><FONT SIZE=3D2>or use something akin to the example below</FONT>
</P>

<P><FONT SIZE=3D2>readMaybeInt :: String -&gt; Maybe Int</FONT>
<BR><FONT SIZE=3D2>readMaybeInt s =3D case reads s of</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [(x,_)] =
-&gt; Just x</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&gt; Nothing</FONT>
</P>
<BR>
<BR>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Hal Daume III [<A =
HREF=3D"mailto:hdaume@ISI.EDU">mailto:hdaume@ISI.EDU</A>]</FONT>
<BR><FONT SIZE=3D2>Sent: 07 February 2002 17:54</FONT>
<BR><FONT SIZE=3D2>To: DK</FONT>
<BR><FONT SIZE=3D2>Cc: haskell@haskell.org</FONT>
<BR><FONT SIZE=3D2>Subject: Re: String manipulation.</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>You should have a look at the Read class.&nbsp; A =
member a of the Read class</FONT>
<BR><FONT SIZE=3D2>has a function read :: String -&gt; a.</FONT>
</P>

<P><FONT SIZE=3D2>so, for example, you can say:</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; (read &quot;5.0&quot;) :: Double</FONT>
</P>

<P><FONT SIZE=3D2>to read this as a double.&nbsp; You could also read =
it as an integer or</FONT>
<BR><FONT SIZE=3D2>something.</FONT>
</P>

<P><FONT SIZE=3D2>If you have a list of strings that you want to =
convert to a list of</FONT>
<BR><FONT SIZE=3D2>integers, you could do:</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; map (\x -&gt; (read x)::Double) [list of =
strings]</FONT>
</P>

<P><FONT SIZE=3D2>Hope that helps.</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;- Hal</FONT>
</P>

<P><FONT SIZE=3D2>--</FONT>
<BR><FONT SIZE=3D2>Hal Daume III</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;&quot;Computer science is no more about =
computers&nbsp;&nbsp;&nbsp; | hdaume@isi.edu</FONT>
<BR><FONT SIZE=3D2>&nbsp; than astronomy is about telescopes.&quot; =
-Dijkstra | www.isi.edu/~hdaume</FONT>
</P>

<P><FONT SIZE=3D2>On Thu, 7 Feb 2002, DK wrote:</FONT>
</P>

<P><FONT SIZE=3D2>&gt; Hello. First of all I am a beginner in Haskell, =
and I must confess I do not yet fully understand it. I need to write a =
program in Haskell, though, and I am having some difficulties... =
</FONT></P>

<P><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; What I would like to ask, is how can I take a =
string from a list, and manipulate it, in order to convert it to an =
integer. Any help would be very appreciated.</FONT></P>

<P><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Thank you very much,</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Dimitris</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
</P>

<P><FONT =
SIZE=3D2>_______________________________________________</FONT>
<BR><FONT SIZE=3D2>Haskell mailing list</FONT>
<BR><FONT SIZE=3D2>Haskell@haskell.org</FONT>
<BR><FONT SIZE=3D2><A =
HREF=3D"http://www.haskell.org/mailman/listinfo/haskell" =
TARGET=3D"_blank">http://www.haskell.org/mailman/listinfo/haskell</A></F=
ONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C1B002.D5619590--