Rohit Sharma | 17 Nov 16:20 2014
Picon

Unable to sequence using 'do' operator

type Parser a = String -> [(a, String)]

Hi,

I am learning haskell and wondering why my definition of firstAndThird does not work with the do operator, however when i try to use the same using bind (firstandThird2) it works as expected. I basically want to return a tuple of first and third character of a string. Can someone please correct me what i might be overseeing?

I have pasted the same code on codetidy in case if the email lose formatting.

Many Thanks,
Rohit

zero :: Parser a
zero = \inp -> []

result :: a -> Parser a
result x = \inp -> [(x, inp)]

item :: Parser Char
item = \inp -> case inp of 
                   [] -> []
                   (x:xs) -> [(x,xs)]

bind :: Parser a -> (a -> Parser b) -> Parser b
p `bind` f = \inp -> concat [ ((f x) inp') | (x, inp') <- p inp]

sat :: (Char -> Bool) -> Parser Char
sat predicate = item `bind` (\x -> if predicate x then result x else zero )

lower :: Parser Char
lower = sat (\x -> 'a' <= x && x <= 'z')

firstAndThird :: Parser (Char, Char) 
firstAndThird = do x <- item 
                        item 
                   y <- item 
                   return (x,y)

firstAndThird2 :: Parser (Char, Char)
firstAndThird2 = item `bind` \x ->
      item `bind` \y ->
      item  `bind` \z ->
      result (x,z)
<div><div dir="ltr">
<div>type Parser a = String -&gt; [(a, String)]</div>
<div><br></div>
<div>Hi,</div>
<div><br></div>
<div>I am learning haskell and wondering why my definition of firstAndThird does not work with the do operator, however when i try to use the same using bind (firstandThird2) it works as expected. I basically want to return a tuple of first and third character of a string. Can someone please correct me what i might be overseeing?</div>
<div><br></div>
<div>I have pasted the same code on codetidy in case if the email lose formatting.</div>
<div>
<a href="http://codetidy.com/5726/">http://codetidy.com/5726/</a><br>
</div>
<div><br></div>
<div>Many Thanks,</div>
<div>Rohit</div>
<div><br></div>
<div>zero :: Parser a</div>
<div>zero = \inp -&gt; []</div>
<div><br></div>
<div>result :: a -&gt; Parser a</div>
<div>result x = \inp -&gt; [(x, inp)]</div>
<div><br></div>
<div>item :: Parser Char</div>
<div>item = \inp -&gt; case inp of&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[] -&gt; []</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(x:xs) -&gt; [(x,xs)]</div>
<div><br></div>
<div>bind :: Parser a -&gt; (a -&gt; Parser b) -&gt; Parser b</div>
<div>p `bind` f = \inp -&gt; concat [ ((f x) inp') | (x, inp') &lt;- p inp]</div>
<div><br></div>
<div>sat :: (Char -&gt; Bool) -&gt; Parser Char</div>
<div>sat predicate = item `bind` (\x -&gt; if predicate x then result x else zero )</div>
<div><br></div>
<div>lower :: Parser Char</div>
<div>lower = sat (\x -&gt; 'a' &lt;= x &amp;&amp; x &lt;= 'z')</div>
<div><br></div>
<div>firstAndThird :: Parser (Char, Char)&nbsp;</div>
<div>firstAndThird = do x &lt;- item&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y &lt;- item&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (x,y)</div>
<div><br></div>
<div>firstAndThird2 :: Parser (Char, Char)</div>
<div>firstAndThird2 = item `bind` \x -&gt;</div>
<div>&nbsp; &nbsp; &nbsp; item `bind` \y -&gt;</div>
<div>&nbsp; &nbsp; &nbsp; item &nbsp;`bind` \z -&gt;</div>
<div>&nbsp; &nbsp; &nbsp; result (x,z)</div>
</div></div>
Rohit Sharma | 15 Nov 13:10 2014
Picon

Bind parser returning wrong return type

Hi All,

Sorry if this is a very lame question but i am a beginner and much appreciate if some one could correct me?

I am reading haskell book and curious why the return type of the bind operator look odd to me

For the given definitions

    type Parser a = String -> [(a, String)]
    
    item :: Parser Char
    item = \inp -> case inp of 
                       [] -> []
                       (x:xs) -> [(x,xs)]
    
    bind :: Parser a -> (a -> Parser b) -> Parser b
    p `bind` f = \inp -> concat [ f x inp' | (x, inp') <- p inp]


when I define z in GHCI as 

    let z = item `bind` (\x -> (\y -> result (x,y))) "Rohit"

the return type is 

    >> :t z
    z :: Parser ([Char], Char)

Question:
(1) Shouldn't the return type of (Char, [Char])? looking at the list comprehension, "(x, inp') <- p inp" should yield -> "('r', "ohit")". Next f x inp' is left associative, so f x should yield character 'r' and pass to the lambda that should return result tuple ('r', "ohit"), but why is it that z type is ([Char], char) :: (x,y)

(2) How can i print the value of z in the above case on the ghci


Many thanks,
Rohit

<div><div dir="ltr">
<div>Hi All,</div>
<div><br></div>
<div>Sorry if this is a very lame question but i am a beginner and much appreciate if some one could correct me?</div>
<div><br></div>
<div>I am reading haskell book and curious why the return type of the bind operator look odd to me</div>
<div><br></div>
<div>For the given definitions</div>
<div><br></div>
<div>&nbsp; &nbsp; type Parser a = String -&gt; [(a, String)]</div>
<div>&nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; item :: Parser Char</div>
<div>&nbsp; &nbsp; item = \inp -&gt; case inp of&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[] -&gt; []</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(x:xs) -&gt; [(x,xs)]</div>
<div>&nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; bind :: Parser a -&gt; (a -&gt; Parser b) -&gt; Parser b</div>
<div>&nbsp; &nbsp; p `bind` f = \inp -&gt; concat [ f x inp' | (x, inp') &lt;- p inp]</div>
<div><br></div>
<div><br></div>
<div>when I define z in GHCI as&nbsp;</div>
<div><br></div>
<div>&nbsp; &nbsp; let z = item `bind` (\x -&gt; (\y -&gt; result (x,y))) "Rohit"</div>
<div><br></div>
<div>the return type is&nbsp;</div>
<div><br></div>
<div>&nbsp; &nbsp; &gt;&gt; :t z</div>
<div>&nbsp; &nbsp; z :: Parser ([Char], Char)</div>
<div><br></div>
<div>Question:</div>
<div>(1) Shouldn't the return type of (Char, [Char])? looking at the list comprehension, "(x, inp') &lt;- p inp" should yield -&gt; "('r', "ohit")". Next f x inp' is left associative, so f x should yield character 'r' and pass to the lambda that should return result tuple ('r', "ohit"), but why is it that z type is ([Char], char) :: (x,y)</div>
<div><br></div>
<div>(2) How can i print the value of z in the above case on the ghci</div>
<div><br></div>
<div><br></div>
<div>Many thanks,</div>
<div>Rohit</div>
<div><br></div>
</div></div>
Roelof Wobben | 11 Nov 17:35 2014
Picon

Re: Are these soloutions all valid and a good use of Haskell

Oke,

But what If I try it on a left folt.
Then it must be the other way.

let's say we have [1 , 2, 3]

so first it's nothing and 1 is taken.
so y could be one and nothuing must stay nothing because it's not the 
last part,
The same for 2 , When it's three nothing must changed to just 3.

Fold is something I find it very difficult to wrap my head around it.
Maybe haskell or any functional proggramming language is not for me,

Roelof

Kees Bleijenberg schreef op 11-11-2014 15:46:
> The program traverses the list  from right to left. The first element that
> acc sees during traversal is the last element of the list.
> The idea is as follows:
> Parameter y is the partial calcresult.
> If y (in your code) is Nothing then acc returns Just x. So the new partial
> result is the current list element in a Just.
> The next time acc is called, y is not Nothing but a Just. In that case acc
> returns the partial calcresult unchanged.
> So, y is Nothing for the last list element and Just <last element of the
> list> for all other list elements.
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x Nothing  = Just x
> acc _ aJust    = aJust
>
> initial :: Maybe a
> initial = Nothing
>
> In your code you don’t need the ‘where’ in the acc definition. Define acc
> for Nothing and for Just.
>
> Kees
>
> ---------------
> Im now trying to make a rfold solution to this problem.
>
> Till now I have this :
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x y = where
>      acc x Nothing  = Just x
>      acc _ j        = j
>
> initial :: Maybe a
> initial = Nothing
>
> So still not working.
>
> Can someone explain to me in small steps how to figure out what the right
> answer is to the acc part.
>
> Roelof
>
>
>
>
>
>
>
>
>
> Roelof Wobben schreef op 11-11-2014 8:20:
> Thanks, this is working well.
>
> But just for understanding
>
> in the part acc x Nothing   x is the input string,
> and in the part _j means the same as (xs:x)
>
> Roelof
>   
>
>
> Alex Hammel schreef op 10-11-2014 22:47:
> It's an indentation issue. Make sure that there are no spaces before the bit
> about 'last5 = foldr acc' etc.
>
> On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben <r.wobben <at> home.nl> wrote:
> Roelof Wobben schreef op 10-11-2014 20:59:
> last5 :: [a] -> Maybe a
>     last5 = foldr acc Nothing where
>         acc x Nothing  = Just x
>         acc _ j        = j
>
> When I enter this in GHCI  I see this error :
>
> Illegal type signature: ‘[a] -> Maybe a last5’
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners <at> haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners <at> haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners <at> haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>

Frank | 11 Nov 07:01 2014
Picon

Haskell and LiveCode

How do the 2 languages compare?

--
P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6

<div><p>How do the 2 languages compare?<span></span><br><br>-- <br>P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6<br></p></div>
Jack Mott | 11 Nov 01:32 2014
Picon

Finding Keith Numbers with Haskell

I'm on day 2 of learning Haskell, and decided to write a program to find Keith Numbers as my first project. So far so good, I have some code that does the job (see below).  First I would just like to ask others to look it over and let me know if I am doing anything particularly wrong or not advised here.

Also, I am using the "isKeithHelper" function because I didn't know how to make use of the filter function directly with isKeith, because isKeith takes two parameters.  I'm pretty sure there is a better way to handle that, any tips?

Lastly, I'd like to make this as efficient as possible. At the moment I am adding [s] to the end of the list in the isKeith function, which I know is O(n) instead of O(1). I don't think there is a way to avoid either that or having to use drop which would have the same downside.  Is this a case where a Sequence or Vector might be more useful? How would I convert this to use a Sequence instead?



import Data.Time.Clock.POSIX
import Data.Digits

isKeith :: Integer -> [Integer] ->  Bool
isKeith n l
    | s == n            = True
    | s > n             = False
    | otherwise         = isKeith n x (l ++ [s])
    where        
        s = sum l

isKeithHelper :: Integer -> Bool
isKeithHelper n = isKeith  n  (digits 10 n)
 

main :: IO ()
main = do        
        start <- getPOSIXTime       
        print (filter isKeithHelper [1000..9999])
        end <- getPOSIXTime
        print (end - start)
<div><div dir="ltr">I'm on day 2 of learning Haskell, and decided to write a program to find Keith Numbers as my first project. So far so good, I have some code that does the job (see below).&nbsp; First I would just like to ask others to look it over and let me know if I am doing anything particularly wrong or not advised here.<div><br></div>
<div>Also, I am using the "isKeithHelper" function because I didn't know how to make use of the filter function directly with isKeith, because isKeith takes two parameters.&nbsp; I'm pretty sure there is a better way to handle that, any tips?</div>
<div><br></div>
<div>Lastly, I'd like to make this as efficient as possible. At the moment I am adding [s] to the end of the list in the isKeith function, which I know is O(n) instead of O(1). I don't think there is a way to avoid either that or having to use drop which would have the same downside.&nbsp; Is this a case where a Sequence or Vector might be more useful? How would I convert this to use a Sequence instead?</div>
<div><br></div>
<div>
<br><div><br></div>
<div>
<div>import Data.Time.Clock.POSIX</div>
<div>import Data.Digits</div>
<div><br></div>
<div>isKeith :: Integer -&gt; [Integer] -&gt; &nbsp;Bool</div>
<div>isKeith n l</div>
<div>&nbsp; &nbsp; | s == n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= True</div>
<div>&nbsp; &nbsp; | s &gt; n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = False</div>
<div>&nbsp; &nbsp; | otherwise &nbsp; &nbsp; &nbsp; &nbsp; = isKeith n x (l ++ [s])</div>
<div>&nbsp; &nbsp; where &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; s = sum l</div>
<div><br></div>
<div>isKeithHelper :: Integer -&gt; Bool</div>
<div>isKeithHelper n = isKeith &nbsp;n &nbsp;(digits 10 n)</div>
<div>&nbsp;</div>
<div><br></div>
<div>main :: IO ()</div>
<div>main = do &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; start &lt;- getPOSIXTime &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; print (filter isKeithHelper [1000..9999])</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; end &lt;- getPOSIXTime</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; print (end - start)</div>
</div>
</div>
</div></div>
Roelof Wobben | 10 Nov 10:50 2014
Picon

Are these soloutions all valid and a good use of Haskell

Hello,

I tried to solve the first problem of the 99 haskell problems on 3 
seperate ways.
The problem is that  I have to find the last item of a list.

The solutions I found with a little help of this mailinglist are :

last2::[a]-> Maybe a;
last2 [] = Nothing;
last2 ([x]) = Just x;
last2 (_:xs) = last2 xs

last3::[a]-> Maybe a;
last3 x
   | null x = Nothing
   | null xs = Just (head x)
   | otherwise = last3 (tail x)
   where xs = tail x

last4::[a]-> Maybe a;
last4 x = case x of
     [] -> Nothing ;
     [x] -> Just x ;
     (_:xs) -> last4 xs

main = print $ (last2 [] )

What do you experts think of the different ways ?

Roelof

Adit Biswas | 9 Nov 18:49 2014
Picon

First Project: Imperative Algorithm Visualization tool

I'm kindoff a beginner in haskell, I've mostly just been exploring the
language, some of the introductory books on the subject and writing
some small programs.
This is one of my first projects I wanted to try out, but I'm feeling
a bit lost on how to go about doing it. I want to build a tool which
takes in inputs describing algorithms, probably in their imperative
form and generate animations using SVG and HTML5 preferably or OpenGL.
I find that being able to visualize the execution of an algorithm
helped me understand the material a few years ago when i started
college but I mostly had to do these on pen and paper.

So my higher level idea on approaching this problem is:
    1. Create a dsl for describing data structures, e.g Link lists,
trees, graphs
    2. Create a dsl for describing each step of the algorithms
manipulating the data structures
    3. The algorithms would be a monadic composition of the step ADTs
    4. Lift the algorithm to some monad which carries out the side
effects of making changes to a visualization.

I'm sorry if I didn't use all the correct terminology for describing
the problem, I'm not completely sure if it's an appropriate approach.
My logic was that Since each state of the visualization is a
sideeffect of executing an algorithm, it would be a seperate monad
like IO, the entire algorithm can be represented as some form of a
graph, I should have data types representing each component of the
graph.

I have no idea how to go about designing a DSL or for designing a
monad which will handle the animations. Could someone help me approach
the solution and what topics I want to look at to understand how to
build the solution. Feel free to let me know if you'd like to help
work on the tool, or if you think I should simplify my project in some
way. This is mostly just an idea for a project and since I'm feeling
pretty beginnerish with the language, i wouldnt love suggestions on a
simpler project which can sorta lead me in a direction in which i can
make this tool in the future. Thank You!

--

-- 
Adit Biswas
Roelof Wobben | 9 Nov 10:10 2014
Picon

invalid type signature last2::: [a] -> a

Hello,

I try to solve the 99 haskell problems on several ways.

But my first try which looks like this :

Last2::[a]-> a
Last2:: last[list]

gives the following error message :

src/Main.hs <at> 1:1-1:6
Invalid type signature: Last2 :: [a] -> a Should be of form <variable> 
:: <type>

What am trying to do is say the input is a list which can be of integers 
or chars so everything is the output can then also be of type everything.

Roelof

Jan Snajder | 3 Nov 18:45 2014
Picon

Re: Lazy state + IO not lazy?

Dear all,

On 10/27/14, Kim-Ee Yeoh <ky3 <at> atamo.com> wrote:

Kim-Ee, many thanks for your reply and sorry for my delayed reaction.

> Consider
>
>    let u = return () :: IO () in
>    expr >> u
>
> where expr ranges over (return undefined), (undefined), and (error "urk").

Ok, so this is what we get:

(1)
return undefined >> return () :: IO ()
==> evaluates to 'IO ()'

(2)
 > undefined >> return ()
*** Exception: Prelude.undefined

(3)
 > error "urk" >> return ()
*** Exception: ur

 > Are their executions surprising?

In my understanding, in a strict state monad, the '>>' operator will 
evaluate the (value,state) pair of the left expression up to the (,) 
constructor. Assuming this is correct, in (1) 'return undefined' gives a 
'(undefined,state)', which is not evaluated further, hence the 
computation continues. In (2), there is no 'return' to create a 
(value,state) pair, so when '>>' tries to evaluate the left expression, 
it evaluates directly to undefined. The same holds for error, which (I 
guess) stops the execution immediately upon being evaluated.

Is my reasoning correct?

> Compare to
>
>    let u = return () :: State Int () in
>    evalState (expr >> u) 0
>
> for both strict and lazy state.

Ok, here we go:

 > Strict.evalState (return undefined >> return () :: Strict.State Int ()) 0
()

 > Strict.evalState (undefined >> return () :: Strict.State Int ()) 0
*** Exception: Prelude.undefined

 > Strict.evalState (error "urk" >> return () :: Strict.State Int ()) 0
*** Exception: urk

 > Lazy.evalState (return undefined >> return () :: Lazy.State Int ()) 0
()
 > Lazy.evalState (undefined >> return () :: Lazy.State Int ()) 0
()
 > Lazy.evalState (error "urk" >> return () :: Lazy.State Int ()) 0
()

So, strict-state monad matches the behavior of 'IO', which is what I 
expected because I know 'IO' is a strict-state monad.

Furthermore, lazy-state monad never inspects the left argument of '>>', 
so all three cases compute.

 > IO above matches one of them. Suppose
> IO's behavior matches the other, what happens?

I guess the IO monad would behave in an undesirable way because we would 
not be able to raise an exception nor catch undefined computations. Is 
that what you meant?

> Now with the definition of mapM in mind, consider the difference
> between your original:
>
>    Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0
>
> and
>
>    Lazy.evalStateT (head `fmap` mapM return [1,undefined]) 0
>
> Did you mean the latter?

No, that's not what I meant. But I assume that in the latter case we 
won't get a bottom because we don't evaluate beyond the spine of the list:

 > Lazy.evalState (head `fmap` mapM return [1,undefined]) 0
1
 > Lazy.evalStateT (head `fmap` mapM return [1,undefined]) 0
1

Is my interpretation correct?

But what confuses me why we get different behavior here:

 >  Lazy.evalState (head `fmap` mapM return (1:undefined)) 0
1
 >  Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0
*** Exception: Prelude.undefined

 From this I conclude that the second monad is strict, whereas the first 
is not. Is that correct? Assuming that is correct, does this mean that 
every monad stacked on top of an IO monad becomes strict??

Here's another example that illustrates this point:
http://pastebin.com/0fAFmufA

Why is (4) not computed in constant space, whereas (3) is?

Finally, what ultimately bothers me is the following:
is it true that if my state monad is built on top of an IO monad I 
cannot lazily consume a result of a mapM computation?

(Here I am assuming, perhaps wrongly, that if '>>=' is non-strict in its 
left argument, then 'sequence' is tail recursive modulo cons. But I'm 
actually not convinced why this should be the case.)

Many thanks for any input!

Cheers,
Jan
Cui Liqiang | 28 Oct 13:00 2014
Picon

Haskell typing question

Thanks for your help, your suggestion works.

But I still don’t quite understand. In the following line:
caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l)),
After applying digitToInt, the type of ‘x’ in the expression above is Int indeed, but Haskell consider the ’10.0’ to be a Int, is it?




----------------------------------------------------------------------------------------------------------------------------
Hi,
I am doing an exercise in Haskell, which is converting a string like ?$123.312? to double value. Below is my code:

module Main where
import Data.Char

caluInt l = foldl1 (\acc x -> acc * 10 + x) (map digitToInt l)
caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l))

convert(x:xs) =
let num = [e | e <- xs, e /= ',']
intPart = takeWhile (/='.') num
decimalPart = tail(dropWhile (/='.') num)
in (caluInt intPart) + (caluDecimal decimalPart)


And I got an error in this line: caluDecimal l = (foldr1 (\x acc -> acc / 10.0 + x) (map digitToInt l)),
which says:
No instance for (Fractional Int) arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Int)
In the first argument of `(+)', namely `acc / 10.0'
In the expression: acc / 10.0 + x
In the first argument of `foldr1', namely
`(\ x acc -> acc / 10.0 + x)'


Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell I want a Fractional?
--
Cui Liqiang


Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell
I want a Fractional?

Because digitToInt means exactly what it says. If you want it to become
something other than Int, apply fromIntegral to its result.
<div>
<div>Thanks for your help, your suggestion works.</div>
<div><br></div>
<div>But I still don&rsquo;t quite understand. In the following line:</div>
<div>
<span class="Apple-tab-span">	</span>caluDecimal l = (foldr1 (\x acc -&gt; acc / 10.0 + x) (map digitToInt l)),</div>
<div>After applying digitToInt, the type of &lsquo;x&rsquo; in the expression above is Int indeed, but Haskell consider the &rsquo;10.0&rsquo; to be a Int, is it?</div>
<div><span><br></span></div>
<div><span><br></span></div>
<div><span><br></span></div>
<div><span><br></span></div>
<div><span>----------------------------------------------------------------------------------------------------------------------------</span></div>
<div>
<div>Hi,</div>
<div>I am doing an exercise in Haskell, which is converting a string like ?$123.312? to double value. Below is my code:</div>
<div><br></div>
<div>module Main where</div>
<div>import Data.Char</div>
<div><br></div>
<div>caluInt l = foldl1 (\acc x -&gt; acc * 10 + x) (map digitToInt l)</div>
<div>caluDecimal l = (foldr1 (\x acc -&gt; acc / 10.0 + x) (map digitToInt l))</div>
<div><br></div>
<div>convert(x:xs) =</div>
<div>let num = [e | e &lt;- xs, e /= ',']</div>
<div>intPart = takeWhile (/='.') num</div>
<div>decimalPart = tail(dropWhile (/='.') num)</div>
<div>in (caluInt intPart) + (caluDecimal decimalPart)</div>
<div><br></div>
<div><br></div>
<div>And I got an error in this line: caluDecimal l = (foldr1 (\x acc -&gt; acc / 10.0 + x) (map digitToInt l)),</div>
<div>which says:</div>
<div>No instance for (Fractional Int) arising from a use of `/'</div>
<div>Possible fix: add an instance declaration for (Fractional Int)</div>
<div>In the first argument of `(+)', namely `acc / 10.0'</div>
<div>In the expression: acc / 10.0 + x</div>
<div>In the first argument of `foldr1', namely</div>
<div>`(\ x acc -&gt; acc / 10.0 + x)'</div>
<div><br></div>
<div><br></div>
<div>Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell I want a Fractional?</div>
<div>--</div>
<div>Cui Liqiang</div>
</div>
<div><br></div>
<div><br></div>
<blockquote type="cite">
<div>Why Haskell insists that 10.0 is a Int? How can I explicitly tell Haskell</div>
<div>I want a Fractional?</div>
</blockquote>
<div><br></div>
<div>Because digitToInt means exactly what it says. If you want it to become</div>
<div>something other than Int, apply fromIntegral to its result.</div>
</div>
Stefan Höck | 28 Oct 05:49 2014
Picon

Cabal-install of local package

Dear list

Like many other I seem to be struggling with some of the concepts of
cabal and cabal-install. For the records: I use cabal-install 1.20.0.3
and ghc 7.8.3 on arch linux.

For starters, I implemented my own idea of an improved prelude, called
the package efa-prelude and the module it exports Efa.Prelude. I then
built the package and installed it using `cabal install --user`.
This worked and a folder for the library was created at
"$HOME/.cabal/lib/x86_64-linux-ghc-7.8.3/efa-prelude-0.1.0".
I also registered the library using `cabal register --user` and
now cabal prints some information about the package when I type
`cabal info efa-prelude`. I am also able to import Efa.Prelude into
ghci.

However, when I now try to use this library in another local project
using a sandbox this time, I can build the project but
when I try to install it into the sandbox I get the following error
message:

    Resolving dependencies...
    cabal: Could not resolve dependencies:
    trying: test-0.1.0 (user goal)
    next goal: efa-prelude (dependency of test-0.1.0)
    Dependency tree exhaustively searched.

I just tried and the install works if I do it outside of a sandbox.
Obviously I am missing something crucial here. Could somebody please point me
in the right direction what is needed to make my own packages available
in a sanboxed install?

Thanks for your time

Stefan


Gmane