(moving to haskell-cafe...)<br><br><div><span class="gmail_quote">On 10/8/07, <b class="gmail_sendername">Johan Tibell</b> &lt;<a href="mailto:johan.tibell@gmail.com">johan.tibell@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 10/8/07, bjornie &lt;<a href="mailto:bjorn.wikstrom@gmail.com">bjorn.wikstrom@gmail.com</a>&gt; wrote:<br>&gt;<br>&gt; Hi! I&#39;m kind of new to functional programming with Haskell, I&#39;m reading a<br>&gt; university course you see. Now I&#39;m having some problems with one of our lab
<br>&gt; assignments, we&#39;re supposed to write a program handling numeric operations<br>&gt; (addition, multiplication, and some functionality like sin and cos).<br>&gt;<br>&gt; I&#39;ve made a data type;<br>&gt; data Expr = Num Double | Add Expr Expr | Mul Expr Expr...
<br>&gt;<br>&gt; I.e for representing the expression:<br>&gt; 2.4 * (4.0 + 1.5)<br>&gt; The Haskell representation is:<br>&gt; Mul (Num 2.4) (Add (Num 4.0) (Num 1.5))<br>&gt;<br>&gt; My problem is to convert a String to an expression like the one above.
<br>&gt; I.e the String: &quot;2.4*(4.0+1.5)&quot;<br>&gt;<br>&gt; If we would use plain Integers there would not be a problem, since the<br>&gt; Prelude function &#39;digitToInt&#39; :: Char -&gt; Int does nearly what I would like
<br>&gt; to do. But I&#39;m stuck on the Doubles! How can I solve this in a smooth way?<br><br>You can use read. Here&#39;s a session from GHC&#39;s interpreter:<br><br>$ ghci<br>Prelude&gt; read &quot;1.0&quot; :: Double
<br>1.0<br><br>Since read is polymorphic (i.e. works for different types) you need to<br>specify the type of the result you want using &quot;:: Double&quot;.</blockquote><div><br>...although if you have something like Num (read x), you don&#39;t need the :: Double since the typechecker can figure it out (the Num constructor has to take a Double).&nbsp; Note that you would use the &#39;read&#39; function to read integer values, too; digitToInt is just for dealing with single Chars, not entire integral values.&nbsp; The read function takes a String representation of anything that is &quot;readable&quot; (any type that is an instance of the Read type class) and converts it into a value of the requested type, so it works for Doubles, Ints, Integers, lists, and so on...
<br><br>Also, if I were you I&#39;d use something other than &quot;Num&quot; as the name of the data constructor for numeric values in your Expr type.&nbsp; Num is already used for something else (it&#39;s the name of the general numeric type class), and although it&#39;s legal to have the same name for both, it might lead to confusion, especially when dealing with error messages.
<br><br>Good luck with learning Haskell and with your course!&nbsp; If you need more help there&#39;s also the <a href="http://haskell.org/haskellwiki/IRC_channel">#haskell IRC channel on freenode.net</a>, where you&#39;ll find lots of friendly people who would be glad to answer your questions.
<br><br>-Brent<br></div></div>