Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Reading Multiple Files and Iterate Function Application
(Thomas Miedema)
2. Re: Reading Multiple Files and Iterate Function Application
(Daniel Fischer)
3. Re: Problem in Gtk2Hs/Glade (Daniel Fischer)
4. Re: Problem in Gtk2Hs/Glade (Stephen Tetley)
5. Longest palindrome (Lyndon Maydwell)
6. Re: Longest palindrome (Thomas Miedema)
----------------------------------------------------------------------
Message: 1
Date: Mon, 11 Oct 2010 19:20:11 +0200
From: Thomas Miedema <[email protected]>
Subject: Re: [Haskell-beginners] Reading Multiple Files and Iterate
Function Application
To: Lorenzo Isella <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
replace
let fl = getAllLengths nums
by
fl <- getAllLengths nums
, since getAllLengths returns a monadic action.
The author of the following book is much better at explaining why this is so
than I am: http://learnonlineyouahaskell.com/
<http://learnyouahaskell.com/>. May
I suggest you read it cover to cover, it's really really good. It is
probably the best way to learn Haskell at the moment, together with trying
out code snippets like you're doing right now.
Regards,
Thomas
On Mon, Oct 11, 2010 at 6:06 PM, Lorenzo Isella <[email protected]>wrote:
> Thanks a lot Daniel, but I am a bit lost (up to not long ago I did not even
> know the existence of a control monad...and some unstructured reading did
> not help).
> Some online research about mapM and fmap led me here
> http://en.wikibooks.org/wiki/Haskell/Category_theory
> and I think I am a bit astray at this point ;-)
>
> Why does my "simple" snippet below raise a number of errors?
>
> Cheers
>
> Lorenzo
>
>
> import Data.Ord
>
> import Data.List
>
> main :: IO ()
>
> main = do
>
> let nums=[1,2]
>
> let fl = getAllLengths nums
>
> putStrLn "fl is, "
> print fl
>
>
>
> filename :: Int -> FilePath
> filename i = "file" ++ show i ++ ".dat"
>
> fileLength :: FilePath -> IO Int
> fileLength file = fmap length (readFile file)
>
> getAllLengths :: [Int] -> IO [Int]
> getAllLengths nums = mapM (fileLength . filename) nums
>
>
>
> On 10/11/2010 05:21 PM, Daniel Fischer wrote:
>
>> On Monday 11 October 2010 16:56:58, Lorenzo Isella wrote:
>>
>>> Dear All,
>>> Another I/O question.
>>> Let us say that you are given a list of files file1.dat,
>>> file2.dat...file10.dat and so on (i.e. every file is indexed by a number
>>> and every file is a single column where every entry is a string without
>>> spaces).
>>> In the snippet below I read file1.dat, convert it to a list and then
>>> print out its length.
>>> Now, how can I iterate the process on file1.dat, file2.dat and file3.dat
>>> and store the lengths in a list?
>>>
>>
>> fileLength :: FilePath -> IO Int
>> fileLength file = fmap length (readFile file)
>>
>> filename :: Int -> FilePath
>> filename i = "file" ++ show i ++ ".dat"
>>
>> getAllLengths :: [Int] -> IO [Int]
>> getAllLengths nums = mapM (fileLength . filename) nums
>>
>>
>> If you want something other than the character count, instead of
>> fileLength
>> use e.g.
>>
>> countLines :: FilePath -> IO Int
>> countLines file = fmap (length . lines) (readFile file)
>>
>> or whatever you're interested in.
>>
>> Another nice thing is often forM (from Control.Monad)
>>
>> forM nums $ \i -> do
>> let filename = "file" ++ show i ++ ".dat"
>> contents<- readFile filename
>> let result = function contents
>> doSomethingOrNot
>> return result
>>
>> I would like to map the file reading and following operations on the
>>> list [1,2.3], but that is giving me a headache.
>>> It is relatively easy to create the file name
>>>
>>> filename="file"++(show i)++".dat" , for i=1,2,3
>>>
>>> but it the the iteration part that is giving me troubles.
>>> Any suggestion is appreciated.
>>> Cheers
>>>
>>> Lorenzo
>>>
>>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101011/91365a84/attachment-0001.html
------------------------------
Message: 2
Date: Mon, 11 Oct 2010 19:38:44 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Reading Multiple Files and Iterate
Function Application
To: Lorenzo Isella <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Monday 11 October 2010 18:06:50, Lorenzo Isella wrote:
> Thanks a lot Daniel, but I am a bit lost (up to not long ago I did not
> even know the existence of a control monad...and some unstructured
> reading did not help).
I think there's a misunderstanding here. Control is the top-level name for
stuff related to control flow, like
Control.Concurrent
for concurrency combinators,
Control.Parallel (and Control.Parallel.Strategies)
for parallelism combinators. Monads (some) are also related to control
flow, so the monad stuff that's not available from the Prelude lives in
Control.Monad and Control.Monad.Whatever (Control.Monad.State,
Control.Monad.Writer, ...)
Control.Monad (part of the standard libraries) provides a lot of general
functions for working with Monads, among them forM (which is "flip mapM").
> Some online research about mapM and fmap led me here
> http://en.wikibooks.org/wiki/Haskell/Category_theory
> and I think I am a bit astray at this point ;-)
>
> Why does my "simple" snippet below raise a number of errors?
> Cheers
>
> Lorenzo
>
>
> import Data.Ord
>
> import Data.List
>
> main :: IO ()
>
> main = do
>
> let nums=[1,2]
>
> let fl = getAllLengths nums
That means fl is the IO-action which gets the file lengths. You want the
result, thus
fl <- getAllLengths nums
to bind the result of that action to the name fl.
>
> putStrLn "fl is, "
> print fl
>
>
> filename :: Int -> FilePath
> filename i = "file" ++ show i ++ ".dat"
>
> fileLength :: FilePath -> IO Int
> fileLength file = fmap length (readFile file)
>
> getAllLengths :: [Int] -> IO [Int]
> getAllLengths nums = mapM (fileLength . filename) nums
------------------------------
Message: 3
Date: Mon, 11 Oct 2010 19:48:22 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Problem in Gtk2Hs/Glade
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Monday 11 October 2010 18:19:24, Tom Hobbs wrote:
> Hi all,
>
> I playing around with simple cell automaton (Conway's Game Of Life).
> I've got working code which prints everything out nicely to the
> terminal, I'm trying to wrap everything in a nice GUI using Glade and
> Gtk2Hs.
>
> In order to create my world, I have a function defined as;
>
> makeWorld :: Int -> [(Int,Int)]
>
> Which accepts an Int, representing the size of the world, then then
> outs the world itself - note the world is always assumed to be square.
> The list contains pairs such that the first element of the pair is
> the index (such as for an array) and the second is the value at that
> position. For right or wrong, that's how my (working) Game Of Life
> has been implemented. The problem comes in the Gtk2Hs code...
>
> main = do
> initGUI
> Just xml <- xmlNew "gameOfLife.glade"
> window <- xmlGetWidget xml castToWindow "mainWindow"
> onDestroy window mainQuit
> quitButton <- xmlGetWidget xml castToButton "quitButton"
> onClicked quitButton $ do
> widgetDestroy window
> setWorldSize <- xmlGetWidget xml castToButton "setWorldSize"
> sizeBox <- xmlGetWidget xml castToEntry "worldSize"
> onClicked setWorldSize $ do
> canvas <- xmlGetWidget xml castToDrawingArea "world"
> let size = (read (get sizeBox entryText) :: Int)
> cellworld <- makeWorld size
> printWorld cellworld
> widgetShowAll window
> mainGUI
>
> The above function does not compile under GHC because;
>
> gameOfLife.hs:27:20:
> Couldn't match expected type `String'
> against inferred type `IO String'
> In the first argument of `read', namely `(get sizeBox entryText)'
> In the expression: (read (get sizeBox entryText) :: Int)
> In the definition of `size':
> size = (read (get sizeBox entryText) :: Int)
get sizeBox entryText
is an IO-action which, when executed gets the entry-text from sizeBox.
The type is IO String.
What you want is the entry-text, hence the result of the IO-action, so you
have to run that action and bind its result to a name, for example
text <- get sizeBox entryText
let size = read text
or, on one line
size <- fmap read (get sizeBox entryText)
>
> gameOfLife.hs:28:2:
> Couldn't match expected type `[(Int, Int)]'
> against inferred type `IO (Int, Int)'
> In a stmt of a 'do' expression: cellworld <- makeWorld size
> In the second argument of `($)', namely
> `do { canvas <- xmlGetWidget xml castToDrawingArea "world";
> let size = (read (get sizeBox entryText) :: Int);
> cellworld <- makeWorld size;
> printWorld cellworld }'
> In a stmt of a 'do' expression:
> onClicked setWorldSize
> $ do { canvas <- xmlGetWidget xml castToDrawingArea "world";
> let size = (read (get sizeBox entryText) :: Int);
> cellworld <- makeWorld size;
> printWorld cellworld }
makeWorld size, on the other hand is not an IO-action but a list (of
(Int,Int) pairs), so you can't use "<-" to bind it to a name in an IO-
block, here you must use let:
let cellworld = makeWorld size
print cellWorld
>
> It seems to be complaining about the call to my makeWorld function,
> although I do not understand why that should be. The line number
> which GHC complains about does correspond to the "cellworld <-
> makeWorld size" line in my main function.
>
> I've not yet worked out how to draw my world in the GUI, the
> "printWorld" function mentioned above just spills the world out to the
> termainal for now.
>
> Does anyone have any ideas?
>
> Many thanks,
>
> Tom
------------------------------
Message: 4
Date: Mon, 11 Oct 2010 18:49:24 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Problem in Gtk2Hs/Glade
To: Tom Hobbs <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I suspect get is provided by GtkHS and runs in the IO monad... try this this
sz <- get sizeBox entryText
let size = (read sz) :: Int
On 11 October 2010 17:19, Tom Hobbs <[email protected]> wrote:
> Hi all,
>
> I playing around with simple cell automaton (Conway's Game Of Life).
> I've got working code which prints everything out nicely to the
> terminal, I'm trying to wrap everything in a nice GUI using Glade and
> Gtk2Hs.
>
> In order to create my world, I have a function defined as;
>
> makeWorld :: Int -> [(Int,Int)]
>
> Which accepts an Int, representing the size of the world, then then
> outs the world itself - note the world is always assumed to be square.
> The list contains pairs such that the first element of the pair is
> the index (such as for an array) and the second is the value at that
> position. For right or wrong, that's how my (working) Game Of Life
> has been implemented. The problem comes in the Gtk2Hs code...
>
> main = do
> initGUI
> Just xml <- xmlNew "gameOfLife.glade"
> window <- xmlGetWidget xml castToWindow "mainWindow"
> onDestroy window mainQuit
> quitButton <- xmlGetWidget xml castToButton "quitButton"
> onClicked quitButton $ do
> widgetDestroy window
> setWorldSize <- xmlGetWidget xml castToButton "setWorldSize"
> sizeBox <- xmlGetWidget xml castToEntry "worldSize"
> onClicked setWorldSize $ do
> canvas <- xmlGetWidget xml castToDrawingArea "world"
> let size = (read (get sizeBox entryText) :: Int)
> cellworld <- makeWorld size
> printWorld cellworld
> widgetShowAll window
> mainGUI
>
> The above function does not compile under GHC because;
>
> gameOfLife.hs:27:20:
> Couldn't match expected type `String'
> against inferred type `IO String'
> In the first argument of `read', namely `(get sizeBox entryText)'
> In the expression: (read (get sizeBox entryText) :: Int)
> In the definition of `size':
> size = (read (get sizeBox entryText) :: Int)
>
> gameOfLife.hs:28:2:
> Couldn't match expected type `[(Int, Int)]'
> against inferred type `IO (Int, Int)'
> In a stmt of a 'do' expression: cellworld <- makeWorld size
> In the second argument of `($)', namely
> `do { canvas <- xmlGetWidget xml castToDrawingArea "world";
> let size = (read (get sizeBox entryText) :: Int);
> cellworld <- makeWorld size;
> printWorld cellworld }'
> In a stmt of a 'do' expression:
> onClicked setWorldSize
> $ do { canvas <- xmlGetWidget xml castToDrawingArea "world";
> let size = (read (get sizeBox entryText) :: Int);
> cellworld <- makeWorld size;
> printWorld cellworld }
>
> It seems to be complaining about the call to my makeWorld function,
> although I do not understand why that should be. The line number
> which GHC complains about does correspond to the "cellworld <-
> makeWorld size" line in my main function.
>
> I've not yet worked out how to draw my world in the GUI, the
> "printWorld" function mentioned above just spills the world out to the
> termainal for now.
>
> Does anyone have any ideas?
>
> Many thanks,
>
> Tom
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Tue, 12 Oct 2010 01:54:23 +0800
From: Lyndon Maydwell <[email protected]>
Subject: [Haskell-beginners] Longest palindrome
To: Biginners Haskell Mailinglist <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Hi Café.
I've been looking at implementing the following algorithm in haskell.
Take the maximum length palindrome from the list of palindromes
generated from the original list by:
Taking each element as a starting point, then expanding either side
while the string is still a palindrome.
I could do this numerically after generating an array from a list, but
I was wondering if there was a more idiomatic way to go about it.
------------------------------
Message: 6
Date: Mon, 11 Oct 2010 20:38:57 +0200
From: Thomas Miedema <[email protected]>
Subject: Re: [Haskell-beginners] Longest palindrome
To: Lyndon Maydwell <[email protected]>
Cc: Biginners Haskell Mailinglist <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
see the section "A naive algorithm for finding palindromes" in this
blogpost:
http://johanjeuring.blogspot.com/2007/08/finding-palindromes.html
On Mon, Oct 11, 2010 at 7:54 PM, Lyndon Maydwell <[email protected]> wrote:
> Hi Café.
>
> I've been looking at implementing the following algorithm in haskell.
>
> Take the maximum length palindrome from the list of palindromes
> generated from the original list by:
> Taking each element as a starting point, then expanding either side
> while the string is still a palindrome.
>
> I could do this numerically after generating an array from a list, but
> I was wondering if there was a more idiomatic way to go about it.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101011/eeb9510e/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 21
*****************************************