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. count function (John Moore)
2. Re: count function (Alexander Dunlap)
3. Re: count function (Robert Ziemba)
4. Re: count function (Daniel Fischer)
5. Re: count function (Felipe Lessa)
6. Re: map question (Will Ness)
7. Re: map question (Will Ness)
8. Re: map question (Will Ness)
9. Re: Re: map question (Deniz Dogan)
----------------------------------------------------------------------
Message: 1
Date: Sun, 18 Oct 2009 15:52:28 +0100
From: John Moore <[email protected]>
Subject: [Haskell-beginners] count function
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
Trying to get aprogram to count all the lines or words from a file. Not
sure about the syntax. Probably missing some.
Here's what I have so far any help in the right direction would be great.
import System.IO
main :: IO ()
main = do
incom <- openFile "file.txt" ReadMode
otcom <- openFile "prob.txt" WriteMode
fCount incom otcom
hClose incom
hClose otcom
fCount incomh otcomh =
do eof <- hIsEof incomh
if eof
then return()
else do c <- hGetCount incom
it tells me parse error on =
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091018/a46cf273/attachment-0001.html
------------------------------
Message: 2
Date: Sun, 18 Oct 2009 08:01:53 -0700
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] count function
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sun, Oct 18, 2009 at 7:52 AM, John Moore <[email protected]> wrote:
> Hi,
> Â Trying to get aprogram to count all the lines or words from a file. Not
> sure about the syntax. Probably missing some.
> Here's what I have so far any help in the right direction would be great.
>
> import System.IO
> main :: IO ()
> main = do
> Â Â Â Â Â Â incom <- openFile "file.txt" ReadMode
> Â Â Â Â Â Â otcom <- openFile "prob.txt" WriteMode
> Â Â Â Â Â Â fCount incom otcom
> Â Â Â Â Â Â hClose incom
> Â Â Â Â Â Â hClose otcom
> Â Â Â Â Â Â fCount incomh otcomh =
> Â Â Â Â Â Â Â Â do eof <- hIsEof incomh
> Â Â Â Â Â Â Â Â Â Â Â Â if eof
> Â Â Â Â Â Â Â Â Â Â Â Â Â then return()
> Â Â Â Â Â Â Â Â Â Â Â Â Â else do c <- hGetCount incom
>
> it tells me parse error on =
>
> John
Add a "where" before fCount - this lets you introduce auxiliary
functions. Alternatively, you could define fCount at the top level.
The way I would count all of the lines in a file is (untested)
fCountLines :: String -> IO Int
fCountLines = length . lines . readFile
Hope that helps.
Alex
------------------------------
Message: 3
Date: Sun, 18 Oct 2009 09:40:48 -0700
From: Robert Ziemba <[email protected]>
Subject: Re: [Haskell-beginners] count function
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap <
[email protected]> wrote:
>
>
> The way I would count all of the lines in a file is (untested)
>
> fCountLines :: String -> IO Int
> fCountLines = length . lines . readFile
>
>
I'm not sure if I did something wrong, but I could not get this to work
because 'readFile' returns an IO String. I tried the following and it
worked.
fileLines str = readFile str >>= return . lines >>= return . length
Is this a reasonable way to count lines?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091018/48f68bb1/attachment-0001.html
------------------------------
Message: 4
Date: Sun, 18 Oct 2009 18:51:41 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] count function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Am Sonntag 18 Oktober 2009 18:40:48 schrieb Robert Ziemba:
> On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap <
>
> [email protected]> wrote:
> > The way I would count all of the lines in a file is (untested)
> >
> > fCountLines :: String -> IO Int
> > fCountLines = length . lines . readFile
>
> I'm not sure if I did something wrong, but I could not get this to work
> because 'readFile' returns an IO String. I tried the following and it
> worked.
>
> fileLines str = readFile str >>= return . lines >>= return . length
>
> Is this a reasonable way to count lines?
Almost. It should be
fileLines str = readFile str >>= return . length . lines
(or
return . length . lines =<< readFile str
or
fmap (length . lines) (readFile str)
or...)
------------------------------
Message: 5
Date: Sun, 18 Oct 2009 15:43:00 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] count function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sun, Oct 18, 2009 at 06:51:41PM +0200, Daniel Fischer wrote:
> fileLines str = readFile str >>= return . length . lines
This kind of construction requires a Monad, which is a lot of power.
> fmap (length . lines) (readFile str)
On the other hand, this only requires a Functor. Yes, in this
particular case the type is going to be IO anyway, but 'fmap'
should be used instead of '>>= return .'.
--
Felipe.
------------------------------
Message: 6
Date: Sun, 18 Oct 2009 20:29:45 +0000 (UTC)
From: Will Ness <[email protected]>
Subject: [Haskell-beginners] Re: map question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Deniz Dogan <deniz.a.m.dogan <at> gmail.com> writes:
>
> `This` syntax is used to make functions work infix, whereas operators
> already are infix. For this reason, it wouldn't make much sense to use
> `this` syntax (what's that called anyways?) on an operator.
It would be a no-op in general, so nobody would use it.
> `(-)` would in some sense be the same thing as just "-", since () is
> used to make operators prefix and `` is used to make functions infix.
exactly, just that it would be a _binary_ (-) .
------------------------------
Message: 7
Date: Sun, 18 Oct 2009 20:40:46 +0000 (UTC)
From: Will Ness <[email protected]>
Subject: [Haskell-beginners] Re: map question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
> >
> > backticks could've been made no-op for operators. What's so wrong with
> > (`:`[])? It'd just be the same as (:[]).
>
> Makes writing the parser more complicated.
> Makes reading code more irritating.
I suspected as much, but if it has some benefits, that would be justified.
> > Except for `-`, where it would finally provide us with possibility to write
> > a shortcut for the ugly (flip (-) 1) as (`-`1).
>
> That's ugly too.
> My favourite is subtract 1.
We could write (hypothetically) 'concatenate' for ++ too, but we don't. What's
so great with writing out long names, that should be processed, mentally, to
get at their meaning, when we have this nice operators syntax which makes the
meaning immediately - visibly - apparent?
The only problem in Haskell syntax is this unary-over-binary (-), so allowing
backticks for operators as well as for regular names would finally provide a
solution for it.
Nobody would use it except for (-) sections. Or even, hypothetically, it could
be used to allow unary operators in the language, in general. That whould be a
mechanism for distinguishing the binary from the unary versions.
Why not? Difficulty of writing a parser is not a valid reason if there's a
clear utility in it. IMHO.
------------------------------
Message: 8
Date: Sun, 18 Oct 2009 20:57:38 +0000 (UTC)
From: Will Ness <[email protected]>
Subject: [Haskell-beginners] Re: map question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Daniel Fischer <daniel.is.fischer <at> web.de> writes:
> Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness:
> > (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside
> > backticks would require it be a binary infix op, causing a type mis-match.
>
> instance (Integral a) => Integral (b -> a) where ...
>
> Evil, yes, but then f `(2 `mod`)` x would type-check.
Any problem that could be introduced for operators could be introduced for the
regular named values as well.
>
> But anyway, there are operators where a backticked section would type-check:
>
> f `(g `.`)` x
>
> That's not good.
Why? Is it not just (g .) f x ? What's the problem with it?
But, the backticked parens syntax is less necessary. What I'd really like is
for (`+`1) syntax to become legal.
Prelude> :t let add=(+) in (`add`2)
let add=(+) in (`add`2) :: (Num a) => a -> a
Are we not supposed to be able to substitute equal for equal in Haskell?
Prelude> :t (`(+)`2)
<interactive>:1:2: parse error on input `('
Prelude> :t (`+`2)
<interactive>:1:2: parse error on input `+'
------------------------------
Message: 9
Date: Sun, 18 Oct 2009 23:04:51 +0200
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Re: map question
To: Will Ness <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2009/10/18 Will Ness <[email protected]>:
> Deniz Dogan <deniz.a.m.dogan <at> gmail.com> writes:
>
>>
>> `This` syntax is used to make functions work infix, whereas operators
>> already are infix. For this reason, it wouldn't make much sense to use
>> `this` syntax (what's that called anyways?) on an operator.
>
> It would be a no-op in general, so nobody would use it.
>
>> `(-)` would in some sense be the same thing as just "-", since () is
>> used to make operators prefix and `` is used to make functions infix.
>
> exactly, just that it would be a _binary_ (-) .
>
So the only case where `(this)` syntax would be useful is where you
want to make GHC understand you mean the binary (-) operator and you
want to use point-free code. Now you have the choice between `(-)` and
subtract, which one will it be? :)
--
Deniz Dogan
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 16, Issue 11
*****************************************