Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  A sort of fold for bits (Mario Lang)
   2.  IO String and String using readFile (Yugesh Kothari)
   3. Re:  IO String and String using readFile (Ian Denhardt)
   4. Re:  IO String and String using readFile (Yugesh Kothari)
   5. Re:  IO String and String using readFile (Theodore Lief Gannon)
   6.  Fwd:  IO String and String using readFile (Yugesh Kothari)


----------------------------------------------------------------------

Message: 1
Date: Wed, 27 Mar 2019 02:08:15 +0100
From: Mario Lang <ml...@delysid.org>
To: beginners@haskell.org
Subject: [Haskell-beginners] A sort of fold for bits
Message-ID: <87bm1xjf9c....@fx.blind.guru>
Content-Type: text/plain; charset=utf-8

Hi.

I have written the following function for "iterating" over all set bits
in a Bits instance:

foldBits :: (Bits bits, Num bits) => (a -> Int -> a) -> a -> bits -> a
foldBits _ a 0 = a
foldBits f a n = foldBits f (f a lsb) (n `clearBit` lsb) where
  lsb = countTrailingZeros n

The type signature mimmicks foldl.
However, composing several calls to foldBits ends up pretty messy:

foldBits f (foldBits f (foldBits f [] value1) value2) value3

So I was thinking, maybe I should just flip the final arguments to make
composition easier:

fooBits :: (Bits bits, Num bits) => (a -> Int -> a) -> bits -> a ->  a
fooBits _ 0 a = a
foldBits f n a = foldBits f (n `clearBit` lsb) (f a lsb) where
  lsb = countTrailingZeros n

fooBits f value3 . fooBits f value2 . fooBits f value1 $ []

This looks more tidy.  However, now that I am no longer mimmicking
foldl, I am sort of at a loss how to name this beast.

iterBits comes to bind, but this isn't really like iterate.

I know, this question might look silly, but good naming conventions seem
to make it easier for me to reason about the code.

Any suggestions?

P.S.: Mimmicking foldl is probably wrong as well, since this isn't
really a fold, is it?

-- 
CYa,
  ⡍⠁⠗⠊⠕


------------------------------

Message: 2
Date: Wed, 27 Mar 2019 09:05:14 +0530
From: Yugesh Kothari <kothariyug...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] IO String and String using readFile
Message-ID:
        <CACSi4wB3egUHgv_NGcR4hCQ_2aH=3qgswr-fpqn1w5m5fdy...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

I am trying to read data from a file.
When I do-
ghci> contents <- readFile "file.txt"
ghci> words contents

Then everything works.

The same thing when done in a .hs file gives an IO String vs [String] error.

Why is that so?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20190327/5d7c81cf/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 26 Mar 2019 23:40:36 -0400
From: Ian Denhardt <i...@zenhack.net>
To: Yugesh Kothari <kothariyug...@gmail.com>, beginners@haskell.org
Subject: Re: [Haskell-beginners] IO String and String using readFile
Message-ID:
        <155365803596.1175.5211347879418303338@localhost.localdomain>
Content-Type: text/plain; charset="utf-8"

It would help to see the complete source file, but I'll hazard a guess
you're doing something like:

main = do
    contents <- readFile "file.txt"
    words contents

At the GHCi prompt, each line can be any expression, and GHCi will
evaluate it and then display it. The two lines in your session aren't
really related.

In contrast, the do block in the source file expects the expression at
the end to be an IO action.

What do you want your program to do when you get to "words contents"?
display it? (If you want to display it, pass it to `print`).

Quoting Yugesh Kothari (2019-03-26 23:35:14)
>    Hi,�
>    I am trying to read data from a file.
>    When I do-
>    ghci> contents <- readFile "file.txt"
>    ghci> words contents
>    Then everything works.
>    The same thing when done in a .hs file gives an IO String vs [String]
>    error.
>    Why is that so?


------------------------------

Message: 4
Date: Wed, 27 Mar 2019 09:18:35 +0530
From: Yugesh Kothari <kothariyug...@gmail.com>
Cc: beginners@haskell.org
Subject: Re: [Haskell-beginners] IO String and String using readFile
Message-ID:
        <CACSi4wAraWef9mVHuk-BpAiRg5OvsE_4Cy4j+ebT3=exwbj...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I see. anyway, my use case is something like this-

I have two functions
fromFile :: [String] -> [Int]
func :: String -> [Int]

I want to use the "words contents" output and pass each element in it to
func and send back [Int] from "fromFile" function (where I originally read
the file.)

Could you suggest a better way to do this?

Quoting Ian Denhardt


On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <i...@zenhack.net> wrote:

> It would help to see the complete source file, but I'll hazard a guess
> you're doing something like:
>
> main = do
>     contents <- readFile "file.txt"
>     words contents
>
> At the GHCi prompt, each line can be any expression, and GHCi will
> evaluate it and then display it. The two lines in your session aren't
> really related.
>
> In contrast, the do block in the source file expects the expression at
> the end to be an IO action.
>
> What do you want your program to do when you get to "words contents"?
> display it? (If you want to display it, pass it to `print`).
>
> Quoting Yugesh Kothari (2019-03-26 23:35:14)
> >    Hi,�
> >    I am trying to read data from a file.
> >    When I do-
> >    ghci> contents <- readFile "file.txt"
> >    ghci> words contents
> >    Then everything works.
> >    The same thing when done in a .hs file gives an IO String vs [String]
> >    error.
> >    Why is that so?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20190327/9e061e46/attachment-0001.html>

------------------------------

Message: 5
Date: Tue, 26 Mar 2019 20:49:39 -0700
From: Theodore Lief Gannon <tan...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] IO String and String using readFile
Message-ID:
        <cajopsub2ssg0du_7eoo0fjmikr1pvfso9u9xc+_t84cqhp8...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'm guessing in the .hs file, you have this in a do block? Something like:

foo = do
    contents <- readFile
    words contents

If so, the problem is the return type of `words`. Each line of the do block
has to have an IO value, and words is a pure function. To lift its result
into the monad, use the aptly-named `pure` function:

foo = do
    contents <- readFile
    pure $ words contents


On Tue, Mar 26, 2019, 8:35 PM Yugesh Kothari <kothariyug...@gmail.com>
wrote:

> Hi,
>
> I am trying to read data from a file.
> When I do-
> ghci> contents <- readFile "file.txt"
> ghci> words contents
>
> Then everything works.
>
> The same thing when done in a .hs file gives an IO String vs [String]
> error.
>
> Why is that so?
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20190326/2a38966f/attachment-0001.html>

------------------------------

Message: 6
Date: Wed, 27 Mar 2019 09:24:40 +0530
From: Yugesh Kothari <kothariyug...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Fwd:  IO String and String using readFile
Message-ID:
        <CACSi4wBCr7_PKto0s-OS+z4iFNjFO75eOHtZQmTuwCFY1=z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Extremely sorry,

The definition of fromFile is String -> [Int] where input parameter is the
name of the file.

---------- Forwarded message ---------
From: Yugesh Kothari <kothariyug...@gmail.com>
Date: Wed, 27 Mar, 2019, 9:18 AM
Subject: Re: [Haskell-beginners] IO String and String using readFile
To:
Cc: <beginners@haskell.org>


I see. anyway, my use case is something like this-

I have two functions
fromFile :: [String] -> [Int]
func :: String -> [Int]

I want to use the "words contents" output and pass each element in it to
func and send back [Int] from "fromFile" function (where I originally read
the file.)

Could you suggest a better way to do this?

Quoting Ian Denhardt


On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <i...@zenhack.net> wrote:

> It would help to see the complete source file, but I'll hazard a guess
> you're doing something like:
>
> main = do
>     contents <- readFile "file.txt"
>     words contents
>
> At the GHCi prompt, each line can be any expression, and GHCi will
> evaluate it and then display it. The two lines in your session aren't
> really related.
>
> In contrast, the do block in the source file expects the expression at
> the end to be an IO action.
>
> What do you want your program to do when you get to "words contents"?
> display it? (If you want to display it, pass it to `print`).
>
> Quoting Yugesh Kothari (2019-03-26 23:35:14)
> >    Hi,�
> >    I am trying to read data from a file.
> >    When I do-
> >    ghci> contents <- readFile "file.txt"
> >    ghci> words contents
> >    Then everything works.
> >    The same thing when done in a .hs file gives an IO String vs [String]
> >    error.
> >    Why is that so?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20190327/670b9613/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 129, Issue 8
*****************************************

Reply via email to