[Haskell-cafe] One liner?

Paul Keir pkeir at dcs.gla.ac.uk
Thu Oct 2 11:43:18 EDT 2008

Thanks, and to Ketil too. I did see past the missing "./foo/". That's certainly a solution I'm happy with, and I didn't know the term eta reduction, so thanks for that too.

-----Original Message-----
From: Mitchell, Neil [mailto:neil.mitchell.2 at credit-suisse.com]
Sent: Thu 02/10/2008 16:26
To: Paul Keir; haskell-cafe at haskell.org
Subject: RE: [Haskell-cafe] One liner?
You can translate this step by step.
main = do dc <- getDirectoryContents "./foo/"
          mapM_ putStrLn dc

Translating out the do notation

main = getDirectoryContents >>= \dc ->
       mapM_ putStrLn dc

Then we can chop out the dc argument, as its \x -> .... x, and can be
removed (eta reduction):

main = getDirectoryContents >>= 
       mapM_ putStrLn

And finally we just remove the newline:

main = getDirectoryContents >>= mapM_ putStrLn  

Alternatively, we can flip the >>= for =<< and write:

main = mapM_ putStrLn =<< getDirectoryContents

This is now one line, and mirrors how you would write the function if it
was pure using function composition.



This material is sales and trading commentary and does not constitute
investment research. Please follow the attached hyperlink to an
important disclaimer 
<outbind://31/www.credit-suisse.com/emea/legal> > 



	From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Paul Keir
	Sent: 02 October 2008 4:20 pm
	To: haskell-cafe at haskell.org
	Subject: [Haskell-cafe] One liner?

	Hi all,
	There's a common little situation I keep bumping up against. I
don't understand where I'm going wrong, so I've made a little example.
It's to do with binding a result to a variable name using "<-". This
code works fine:
	module Main where
	import System.Directory (getDirectoryContents)
	main = do dc <- getDirectoryContents "./foo/"
	          mapM_ putStrLn dc
	But if I try to avoid the use of the bind to "dc", I fail:
	mapM_ putStrLn (getDirectoryContents "./foo/")
	I've tried using map instead of mapM_, and inserted "return"s
here and there, but no luck. Can anyone tell me where and why I'm going
wrong? The error message is below.
	Couldn't match expected type `[String]'
	       against inferred type `IO [FilePath]'
	In the second argument of `mapM_', namely
	    `(getDirectoryContents "./foo/")'
	In the expression: mapM_ putStrLn (getDirectoryContents
	In the definition of `main':
	    main = mapM_ putStrLn (getDirectoryContents "./foo/")

Please access the attached hyperlink for an important electronic communications disclaimer: 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081002/6e64efd6/attachment.htm

More information about the Haskell-Cafe mailing list