Barry DeZonia | 25 May 19:42 2015
Picon

Can't figure out my error here

Hello,

I have a small piece of code that does not compile and I'm having trouble figuring out why.

Here is the relevant snippet:

readChunks :: Handle -> [String] -> IO [String]
readChunks handle accum = do
  chunk <- readHeaderChunk handle
  if isLast chunk
    then return (accum ++ chunk)
    else return (readChunks handle (accum ++ chunk))

isLast :: [String] -> Bool

readHeaderChunk :: Handle -> IO [String]

And here is the single compiler error:

hacks.hs:48:18:
    Couldn't match expected type `[String]'
                with actual type `IO [String]'
    In the return type of a call of `readChunks'
    In the first argument of `return', namely
      `(readChunks handle (accum ++ chunk))'
    In the expression: return (readChunks handle (accum ++ chunk))

What I'm most confused about is that chunk is passed to isLast as a [String] with no compiler error but cannot be passed to accum ++ chunk that way. Or so it seems. Can someone she some light on this? Thanks.
<div><div dir="ltr">Hello,<div><br></div>
<div>I have a small piece of code that does not compile and I'm having trouble figuring out why.</div>
<div><br></div>
<div>Here is the relevant snippet:</div>
<div><br></div>
<div>readChunks :: Handle -&gt; [String] -&gt; IO [String]</div>
<div>readChunks handle accum = do</div>
<div>&nbsp; chunk &lt;- readHeaderChunk handle</div>
<div>&nbsp; if isLast chunk</div>
<div>&nbsp; &nbsp; then return (accum ++ chunk)</div>
<div>&nbsp; &nbsp; else return (readChunks handle (accum ++ chunk))</div>
<div><br></div>
<div><div>isLast :: [String] -&gt; Bool</div></div>
<div><br></div>
<div><div>readHeaderChunk :: Handle -&gt; IO [String]</div></div>
<div><br></div>
<div>And here is the single compiler error:</div>
<div><br></div>
<div>
<div>hacks.hs:48:18:</div>
<div>&nbsp; &nbsp; Couldn't match expected type `[String]'</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with actual type `IO [String]'</div>
<div>&nbsp; &nbsp; In the return type of a call of `readChunks'</div>
<div>&nbsp; &nbsp; In the first argument of `return', namely</div>
<div>&nbsp; &nbsp; &nbsp; `(readChunks handle (accum ++ chunk))'</div>
<div>&nbsp; &nbsp; In the expression: return (readChunks handle (accum ++ chunk))</div>
</div>
<div><br></div>
<div>What I'm most confused about is that chunk is passed to isLast as a [String] with no compiler error but cannot be passed to accum ++ chunk that way. Or so it seems. Can someone she some light on this? Thanks.</div>
</div></div>
Dananji Liyanage | 27 May 10:15 2015
Picon

Data.Vector in Haskell

Hi All,

I'm trying to extract elements from a Vector using 'head' and 'tail' function in Data.Vector package, but it gives me the following error now.

<interactive>:41:18:
    Couldn't match expected type ‘Data.Vector.Vector a’
                with actual type ‘vector-0.10.9.1:Data.Vector.Vector Int’
    NB: ‘Data.Vector.Vector’
          is defined in ‘Data.Vector’ in package ‘vector-0.10.12.3’
        ‘vector-0.10.9.1:Data.Vector.Vector’
          is defined in ‘Data.Vector’ in package ‘vector-0.10.9.1’
    Relevant bindings include it :: a (bound at <interactive>:41:1)
    In the first argument of ‘Data.Vector.head’, namely ‘it’
    In the expression: Data.Vector.head it

It was working fine before.

Any help would be appreciated. Thanks in advance! 

--
Regards,
Dananji Liyanage
<div><div dir="ltr">
<div class="gmail_default">Hi All,</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">I'm trying to extract elements from a Vector using 'head' and 'tail' function in Data.Vector package, but it gives me the following error now.</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">
<div class="gmail_default">&lt;interactive&gt;:41:18:</div>
<div class="gmail_default">&nbsp; &nbsp; Couldn't match expected type &lsquo;Data.Vector.Vector a&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with actual type &lsquo;vector-0.10.9.1:Data.Vector.Vector Int&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; NB: &lsquo;Data.Vector.Vector&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is defined in &lsquo;Data.Vector&rsquo; in package &lsquo;vector-0.10.12.3&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; &nbsp; &lsquo;vector-0.10.9.1:Data.Vector.Vector&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is defined in &lsquo;Data.Vector&rsquo; in package &lsquo;vector-0.10.9.1&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; Relevant bindings include it :: a (bound at &lt;interactive&gt;:41:1)</div>
<div class="gmail_default">&nbsp; &nbsp; In the first argument of &lsquo;Data.Vector.head&rsquo;, namely &lsquo;it&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; In the expression: Data.Vector.head it</div>
</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">It was working fine before.</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">Any help would be appreciated. Thanks in advance!&nbsp;</div>
<div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div><div>Regards, <br>Dananji Liyanage</div></div></div></div></div>
</div></div>
Dananji Liyanage | 27 May 09:01 2015
Picon

Working with Haskell matrices

Hi All,

I'm implementing a puzzle where the input is given as a matrix, using Data.Matrix package in Haskell.

How do I implement similar functions to `union`, which are available for lists on a matrix? 


--
Regards,
Dananji Liyanage
<div><div dir="ltr">
<div class="gmail_default">Hi All,</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">I'm implementing a puzzle where the input is given as a matrix, using Data.Matrix package in Haskell.</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">How do I implement similar functions to `union`, which are available for lists on a matrix?&nbsp;</div>
<div><br></div>
<div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div><div>Regards, <br>Dananji Liyanage</div></div></div></div></div>
</div></div>
Shishir Srivastava | 28 May 15:57 2015
Picon

Continuations

Hi, 

Reading on continuation I've came across this new style of creating the functions which I guess is not very clear in how it works

---------------
Prelude> let add_cps x y = \k -> k (x+y)
Prelude> add_cps 3 4 $ print 
7
---------------

I have some questions as to 
1) what is the role of variable 'k' and what eventually happens to it.
2) How does print work after the $ because there is clearly no parameter being passed to it.

Thanks,
Shishir Srivastava

<div><div dir="ltr">
<div>Hi,&nbsp;</div>
<div><br></div>
<div>Reading on continuation I've came across this new style of creating the functions which I guess is not very clear in how it works</div>
<div><br></div>
<div>---------------</div>
<div>Prelude&gt; let add_cps x y = \k -&gt; k (x+y)<br>
</div>
<div>
<div>Prelude&gt; add_cps 3 4 $ print&nbsp;</div>
<div>7</div>
</div>
<div>---------------<br>
</div>
<div><br></div>
<div>I have some questions as to&nbsp;</div>
<div>1) what is the role of variable 'k' and what eventually happens to it.</div>
<div>2) How does print work after the $ because there is clearly no parameter being passed to it.</div>
<div><br></div>
<div>Thanks,</div>
<div><div class="gmail_signature"><div dir="ltr">Shishir Srivastava<br><br>
</div></div></div>
</div></div>
Dananji Liyanage | 25 May 08:44 2015
Picon

Using IO values for computations

Hi All,

I'm writing a code, where the input is read from a text file using:  
readValues = readFile "Input.txt"

Since the type of this is 'IO String', I can't use this in the consequent functions.

For an example: I want to split this as follows within another function

extractInput url method template
  | isURI url == True = getList values components
  | otherwise = []
  where components = splitTemplate readValues
        values = getURL (splitURL url) method

This gives the following error:

 Couldn't match type ‘IO String’ with ‘[Char]’
    Expected type: String
      Actual type: IO String

How can I solve this?

Thanks in advance! 

--
Regards,
Dananji Liyanage
<div><div dir="ltr">
<div class="gmail_default">Hi All,</div>
<div class="gmail_default"><br></div>
<div class="gmail_default"><span>I'm writing a code, where the input is read from a text file using: &nbsp;</span></div>
<div class="gmail_default">readValues =&nbsp;readFile "Input.txt"</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">Since the type of this is 'IO String', I can't use this in the consequent functions.</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">For an example: I want to split this as follows within another function</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">
<div class="gmail_default">extractInput url method template</div>
<div class="gmail_default">&nbsp; | isURI url == True = getList values components</div>
<div class="gmail_default">&nbsp; | otherwise = []</div>
<div class="gmail_default">&nbsp; where components = splitTemplate readValues</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; &nbsp; values = getURL (splitURL url) method</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">This gives the following error:</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">
<div class="gmail_default">&nbsp;Couldn't match type &lsquo;IO String&rsquo; with &lsquo;[Char]&rsquo;</div>
<div class="gmail_default">&nbsp; &nbsp; Expected type: String</div>
<div class="gmail_default">&nbsp; &nbsp; &nbsp; Actual type: IO String</div>
</div>
</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">How can I solve this?</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">Thanks in advance!&nbsp;</div>
<div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div><div>Regards, <br>Dananji Liyanage</div></div></div></div></div>
</div></div>
René Klačan | 24 May 17:43 2015
Picon

Crawling page and making json out of it

Hi all,

I have a small example that opens page and creates a json out of it 


It works correctly and it returns desired results.

But I am sure there is a better way (or more Haskell way) how to write it. Especially I don't really like how *getInfo* function looks like.

Get you please give me an advice how to make it more ellegant or tell me what I'm doing wrong.

Thanks

Rene
<div><div dir="ltr">Hi all,<div><br></div>
<div>I have a small example that opens page and creates a json out of it&nbsp;</div>
<div><br></div>
<div>
<a href="https://gist.github.com/reneklacan/262eef04a3bc67607dd8">https://gist.github.com/reneklacan/262eef04a3bc67607dd8</a><br>
</div>
<div><br></div>
<div>It works correctly and it returns desired results.</div>
<div><br></div>
<div>But I am sure there is a better way (or more Haskell way) how to write it. Especially I don't really like how *getInfo* function looks like.</div>
<div><br></div>
<div>Get you please give me an advice how to make it more ellegant or tell me what I'm doing wrong.</div>
<div><br></div>
<div>Thanks</div>
<div><br></div>
<div>Rene</div>
</div></div>
Mike Meyer | 23 May 21:38 2015

ClassyPrelude vs. Haskell.Language.Interpreter

I'm trying to run interpreted code via ClassyPrelude, and getting some results that make me suspect a bug in the Prelude's type system. Or maybe the interpreter.

Anyway, here's a bit of code that works as expected:

{-# LANGUAGE NoImplicitPrelude #-}

import ClassyPrelude
import Language.Haskell.Interpreter

main :: IO ()
main = do
  fun <- runInterpreter $ makeFun "reverse"
  case fun of
   Left e -> print e
   Right f -> readFile "/etc/motd" >>= hPut stdout . f


makeFun expr = do
  set [languageExtensions := [NoImplicitPrelude]] 
  setImportsQ [("ClassyPrelude", Nothing)]
  interpret expr (as :: Text -> Text)


I don't think I can simplify this any further. It works as expected, and also works as expected, and prints out the contents of /etc/motd reversed.

However, if you change the type signature in the last line from Text -> Text to LText -> Ltext (to get lazy text), you get no output. But if you change the function in the first line after main from "reverse" to "id", it works.

So far, it might be an issue with lazy IO. However, change the type signature in the last line to LText -> Text. In this case, there is no output for either value of the expression.  I expect an error in this case, as neither id nor reverse should be able to have the type LText -> Text!

So, is there something I missed in either ClassyPrelude or the Interpreter? Or is this a subtle interaction, in which case can someone suggest a workaround? Or have I found a bug in one of the two?
<div><div dir="ltr">I'm trying to run interpreted code via ClassyPrelude, and getting some results that make me suspect a bug in the Prelude's type system. Or maybe the interpreter.<div><br></div>
<div>Anyway, here's a bit of code that works as expected:</div>
<div><br></div>
<div>
<div>{-# LANGUAGE NoImplicitPrelude #-}</div>
<div><br></div>
<div>import ClassyPrelude</div>
<div>import Language.Haskell.Interpreter</div>
<div><br></div>
<div>main :: IO ()</div>
<div>main = do</div>
<div>&nbsp; fun &lt;- runInterpreter $ makeFun "reverse"</div>
<div>&nbsp; case fun of</div>
<div>&nbsp; &nbsp;Left e -&gt; print e</div>
<div>&nbsp; &nbsp;Right f -&gt; readFile "/etc/motd" &gt;&gt;= hPut stdout . f</div>
<div><br></div>
<div><br></div>
<div>makeFun expr = do</div>
<div>&nbsp; set [languageExtensions := [NoImplicitPrelude]]&nbsp;</div>
<div>&nbsp; setImportsQ [("ClassyPrelude", Nothing)]</div>
<div>&nbsp; interpret expr (as :: Text -&gt; Text)</div>
</div>
<div><br></div>
<div><br></div>
<div>I don't think I can simplify this any further. It works as expected, and also works as expected, and prints out the contents of /etc/motd reversed.</div>
<div><br></div>
<div>However, if you change the type signature in the last line from Text -&gt; Text to LText -&gt; Ltext (to get lazy text), you get no output. But if you change the function in the first line after main from "reverse" to "id", it works.</div>
<div><br></div>
<div>So far, it might be an issue with lazy IO. However, change the type signature in the last line to LText -&gt; Text. In this case, there is no output for either value of the expression.&nbsp; I expect an error in this case, as neither id nor reverse should be able to have the type LText -&gt; Text!</div>
<div><br></div>
<div>So, is there something I missed in either ClassyPrelude or the Interpreter? Or is this a subtle interaction, in which case can someone suggest a workaround? Or have I found a bug in one of the two?</div>
</div></div>
Roelof Wobben | 23 May 08:58 2015
Picon

Couldn't match expected type `(a0 -> [a0]) -> [a0]', with actual type `[a0]'

Hello,

For some reasons my file's are corrupted.
I had repair them with success except this one.

Here is the code :

toDigits  number
   | number <= 0   = []
   | otherwise     = toDigits' [] number
   where
     toDigits' acc 0  = acc
     toDigits' acc number   = ((number `mod` 10 ): acc) (toDigits' 
(number `mod` 10))

main = print $ toDigits 123

and here is the error:

Couldn't match expected type `(a0 -> [a0]) -> [a0]'
                 with actual type `[a0]'
     The function `(number `mod` 10) : acc' is applied to one argument,
     but its type `[a0]' has none
     In the expression:
       ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
     In an equation for toDigits':
         toDigits' acc number
           = ((number `mod` 10) : acc) (toDigits' (number `mod` 10))

Roelof

---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com

Vaibhav Goel | 20 May 13:47 2015

Question regarding CIS 194 Homework Assignment 1

My apologies, this is only tangentially related to Haskell and I am not
looking for a solution. I am confused with the assignment itself and
hence asking here.

The assignment
(https://www.seas.upenn.edu/~cis194/spring13/hw/01-intro.pdf) says:

====== BEGIN
  Double the value of every second digit beginning from the right.
That is, the last digit is unchanged; the second-to-last digit is dou-
bled; the third-to-last digit is unchanged; and so on. For example,
[1,3,8,6]
becomes
[2,3,16,6]

  Add the digits of the doubled values and the undoubled dig-
its from the original number. For example,
[2,3,16,6]
becomes
2+3+1+6+6 = 18
======== END

Firstly, I am confused as to how the doubled values are being added to
the undoubled number in the above example. It looks like only the
individual numbers of the doubled values are being added

Secondly, later on in the assignment:

======== BEGIN
Example
:
validate 4012888888881881 = True
Example
:
validate 4012888888881882 = False

========= END

If we are to follow the algorithm described (double the value of "every
second digit" beginning from the right, last digit unchanged", then the
above numbers are identical EXCEPT For the last digits (1 and 2)

Any help is appreciated in decoding these instructions.  Again please do
not provide Haskell code, since I want to attempt to write it myself.  I
am just looking for help with the algorithm. 

Regards,
Konstantin Saveljev | 18 May 14:03 2015
Picon

profiling :: memory allocation

Hello,

I'm trying to figure out how to reduce the allocation rate in my program. But I'm stuck with the profile report which has the top allocator to be shown like this:

COST CENTRE      MODULE             %time %alloc
>>=                           Internal                  20.5   22.6

I do not understand what it actually means? Can anyone explain to me or is there anything I can do so it shows some more information (instead of showing the bind operator >>=) ?

Thanks,
Konstantin
<div><div dir="ltr">Hello,<div><br></div>
<div>I'm trying to figure out how to reduce the allocation rate in my program. But I'm stuck with the profile report which has the top allocator to be shown like this:</div>
<div><br></div>
<div>
<div>COST CENTRE &nbsp; &nbsp; &nbsp;MODULE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; %time %alloc</div>
<div>&gt;&gt;= &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Internal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20.5 &nbsp; 22.6<br>
</div>
</div>
<div><br></div>
<div>I do not understand what it actually means? Can anyone explain to me or is there anything I can do so it shows some more information (instead of showing the bind operator &gt;&gt;=) ?</div>
<div><br></div>
<div>Thanks,</div>
<div>Konstantin</div>
</div></div>
Dananji Liyanage | 18 May 13:23 2015
Picon

Iterating through lists

Hi All,

I'm building a 9x9 grid like list, and I want to extract each column of that grid.

My input is a list of integers as follows;

input = [0, 0, 0, 0, 0, 0, 4, 0, 9,
0, 0, 0, 0, 8, 0, 0, 5, 0,
7, 0, 2, 4, 5, 3, 6, 0, 0,
6, 7, 0, 0, 0, 1, 5, 0, 2,
2, 0, 8, 7, 0, 4, 3, 0, 1,
9, 0, 4, 5, 0, 0, 0, 8, 6,
0, 0, 6, 3, 1, 9, 8, 0, 7,
0, 2, 0, 0, 7, 0, 0, 0, 0,
1, 0, 7, 0, 0, 0, 0, 0, 0]

I tried list comprehension with 'take' function in lists as follows;

columns xs = [x | x <- (take 1 xs)] : columns (drop 9 xs) --> columns input

This extracts only the first column. How I can go for the other columns?

--
Regards,
Dananji Liyanage
<div><div dir="ltr">
<div class="gmail_default">Hi All,</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">I'm building a 9x9 grid like list, and I want to extract each column of that grid.</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">My input is a list of integers as follows;</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">
<div class="gmail_default">input = [0, 0, 0, 0, 0, 0, 4, 0, 9,</div>
<div class="gmail_default">
<span class="">	</span>0, 0, 0, 0, 8, 0, 0, 5, 0,</div>
<div class="gmail_default">
<span class="">	</span>7, 0, 2, 4, 5, 3, 6, 0, 0,</div>
<div class="gmail_default">
<span class="">	</span>6, 7, 0, 0, 0, 1, 5, 0, 2,</div>
<div class="gmail_default">
<span class="">	</span>2, 0, 8, 7, 0, 4, 3, 0, 1,</div>
<div class="gmail_default">
<span class="">	</span>9, 0, 4, 5, 0, 0, 0, 8, 6,</div>
<div class="gmail_default">
<span class="">	</span>0, 0, 6, 3, 1, 9, 8, 0, 7,</div>
<div class="gmail_default">
<span class="">	</span>0, 2, 0, 0, 7, 0, 0, 0, 0,</div>
<div class="gmail_default">
<span class="">	</span>1, 0, 7, 0, 0, 0, 0, 0, 0]</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">I tried list comprehension with 'take' function in lists as follows;</div>
<div class="gmail_default"><br></div>
<div class="gmail_default">
<div class="gmail_default">columns xs = [x | x &lt;- (take 1 xs)] : columns (drop 9 xs) --&gt; columns input</div>
<div><br></div>
<div>This extracts only the first column. How I can go for the other columns?</div>
</div>
</div>
<div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div><div>Regards, <br>Dananji Liyanage</div></div></div></div></div>
</div></div>

Gmane