Personal tools

Talk:Hitchhikers guide to Haskell

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Discussing code indentation for nice cut-and-paste.)
 
(Solution)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
== Code Indentation: ==
 
== Code Indentation: ==
Code is most easily specified by indenting it by a space in the wiki source:
+
Haskell code should be indented using the <haskell> tag:
   
-- This is some code.
+
http://www.haskell.org/haskellwiki/HaskellWiki:Syntax_highlighting
sq x = x*x
 
   
If two spaces are used to indent, then the code is visually indented:
+
This also allows for proper cut and paste properties.
   
-- Indented code.
+
P.S. I am really enjoying this article! It provides a very nice practical introduction to Haskell. Thanks! [[User:Mforbes|Mforbes]] 10:08, 17 April 2006 (UTC)
sq x = x*x
 
   
However, if two spaces are used, then cutting and pasting the code sample will insert a spurious space that breaks the 2D Haskell layout. Unless there is a way of indenting code visually while preserving the cut an paste layout properties, it seems that one should only use a single space to indent code.
 
   
(P.S. I am really enjoying this article! It provides a very nice practical introduction to Haskell. [[User:Mforbes|Mforbes]] 09:54, 17 April 2006 (UTC))
+
== Problems ==
  +
  +
When I try to compile, or load into ghci, the following code I get get this error:
  +
  +
cd-fit.hs:8:13:
  +
No instance for (Text.Parsec.Prim.Stream s m Char)
  +
arising from a use of `many' at cd-fit.hs:8:13-27
  +
Possible fix:
  +
add an instance declaration for (Text.Parsec.Prim.Stream s m Char)
  +
In a stmt of a 'do' expression: dirs <- many dirAndSize
  +
In the expression:
  +
do dirs <- many dirAndSize
  +
eof
  +
return dirs
  +
In the definition of `parseInput':
  +
parseInput = do dirs <- many dirAndSize
  +
eof
  +
return dirs
  +
Failed, modules loaded: none.
  +
  +
And I panic.
  +
  +
Code:
  +
<haskell>
  +
module Main where
  +
  +
import Text.ParserCombinators.Parsec
  +
  +
data Dir = Dir Int String deriving Show
  +
  +
parseInput =
  +
do dirs <- many dirAndSize
  +
eof
  +
return dirs
  +
  +
dirAndSize =
  +
do size <- many1 digit
  +
spaces
  +
dir_name <- anyChar `manyTill` newline
  +
return (Dir (read size) dir_name)
  +
  +
main = do input <- getContents
  +
putStrLn $ "DEBUG: got input " ++ input
  +
-- compute solution and print it
  +
</haskell>
  +
  +
/Liem
  +
  +
== Solution ==
  +
I solved the above problem by adding a signature to one function:
  +
  +
<haskell>
  +
parseInput :: Parser [Dir]
  +
parseInput = ...
  +
</haskell>
  +
  +
If someone explained ''why'' the signature is required, I could die happy.
  +
  +
[[User:Mgm7734|Mgm7734]] 23:29, 30 July 2011 (UTC)
  +
  +
Its because of the [[Monomorphism_restriction]]
  +
  +
== Knapsack problem? ==
  +
  +
Surely this is actually an example of bin packing? No?

Latest revision as of 17:19, 4 June 2012

Contents

[edit] 1 Code Indentation:

Haskell code should be indented using the <haskell> tag:

http://www.haskell.org/haskellwiki/HaskellWiki:Syntax_highlighting

This also allows for proper cut and paste properties.

P.S. I am really enjoying this article! It provides a very nice practical introduction to Haskell. Thanks! Mforbes 10:08, 17 April 2006 (UTC)


[edit] 2 Problems

When I try to compile, or load into ghci, the following code I get get this error:

   cd-fit.hs:8:13:
   No instance for (Text.Parsec.Prim.Stream s m Char)
     arising from a use of `many' at cd-fit.hs:8:13-27
   Possible fix:
     add an instance declaration for (Text.Parsec.Prim.Stream s m Char)
   In a stmt of a 'do' expression: dirs <- many dirAndSize
   In the expression:
       do dirs <- many dirAndSize
          eof
          return dirs
   In the definition of `parseInput':
       parseInput = do dirs <- many dirAndSize
                       eof
                       return dirs
   Failed, modules loaded: none.

And I panic.

Code:

module Main where
 
import Text.ParserCombinators.Parsec
 
data Dir = Dir Int String deriving Show
 
parseInput = 
  do dirs <- many dirAndSize
     eof
     return dirs
 
dirAndSize = 
  do size <- many1 digit
     spaces
     dir_name <- anyChar `manyTill` newline
     return (Dir (read size) dir_name)
 
main = do input <- getContents
          putStrLn $ "DEBUG: got input " ++ input
          -- compute solution and print it

/Liem

[edit] 3 Solution

I solved the above problem by adding a signature to one function:

parseInput :: Parser [Dir]       
parseInput = ...

If someone explained why the signature is required, I could die happy.

Mgm7734 23:29, 30 July 2011 (UTC)

Its because of the Monomorphism_restriction

[edit] 4 Knapsack problem?

Surely this is actually an example of bin packing? No?