Send Beginners mailing list submissions to
        [email protected]

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
        [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.  How do I map a String and a IO String? (Jona Ekenberg)
   2. Re:  How do I map a String and a IO String? (Francesco Ariis)
   3. Re:  How do I map a String and a IO String? (Jona Ekenberg)
   4.  order of things in a file.hs? (Silent Leaf)
   5. Re:  order of things in a file.hs? (Norbert Melzer)
   6. Re:  order of things in a file.hs? (Jona Ekenberg)


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

Message: 1
Date: Thu, 29 Jun 2017 14:33:22 +0200
From: Jona Ekenberg <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] How do I map a String and a IO String?
Message-ID:
        <calveeuchyyj2rsocn54ebgk00pudsp3ttmsalzx69bm2f5z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I want to replace lines with the content of a file if the line is a filename

> {-# LANGUAGE LambdaCase #-}
> test = do
>   putStrLn $ concatMap (\l -> l ++ "\n")
>            $ map (\case l
>                           | (isPrefixOf "./" l) -> readFile l
>                           | otherwise           -> l)

But since l is a String and readFile gives an IO String this doesn't work.
How should I get around this?

Kind regards,
Jona
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/69762fa5/attachment-0001.html>

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

Message: 2
Date: Thu, 29 Jun 2017 15:16:02 +0200
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] How do I map a String and a IO
        String?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On Thu, Jun 29, 2017 at 02:33:22PM +0200, Jona Ekenberg wrote:
> I want to replace lines with the content of a file if the line is a filename

Hello Jonas, you want to user `return`:

    λ> :t return
    return :: Monad m => a -> m a

which lifts a simple value inside a monad (in this case, a -> IO a), like
this:

    lineOrIO :: String -> IO String
    lineOrIO cs | (isPrefixOf "./" cs) = readFile cs
                | otherwise            = return cs

If this is not a school assignment, consider replacing `isPrefixOf "./"`
with something from `System.Directory`.

Does this help?


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

Message: 3
Date: Thu, 29 Jun 2017 16:15:16 +0200
From: Jona Ekenberg <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How do I map a String and a IO
        String?
Message-ID:
        <calveeudu03kg2dvjsz7exzmtfi-rnc5uycv_znhnfnh66gg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

2017-06-29 15:16 GMT+02:00 Francesco Ariis <[email protected]>:

> On Thu, Jun 29, 2017 at 02:33:22PM +0200, Jona Ekenberg wrote:
> > I want to replace lines with the content of a file if the line is a
> filename
>
> Hello Jonas, you want to user `return`:
>
>     λ> :t return
>     return :: Monad m => a -> m a
>
> which lifts a simple value inside a monad (in this case, a -> IO a), like
> this:
>
>     lineOrIO :: String -> IO String
>     lineOrIO cs | (isPrefixOf "./" cs) = readFile cs
>                 | otherwise            = return cs
>
> If this is not a school assignment, consider replacing `isPrefixOf "./"`
> with something from `System.Directory`.
>
> Does this help?


Thank you for your help Francesco!

I tried writing it like this:

> lineOrIo :: String -> IO String
> lineOrIo cs | (isPrefixOf "./" cs) = readFile cs
>             | otherwise            = return cs
>
> printLines path = do
>   file <- readFile path
>   lines <- map lineOrIo (lines file)
>   print lines

But when evaluating I get this error:

PrintComments.lhs:20:14-38: error: …
    • Couldn't match type ‘[]’ with ‘IO’
      Expected type: IO (IO String)
        Actual type: [IO String]
    • In a stmt of a 'do' block: lines <- map lineOrIo (lines file)
      In the expression:
        do { file <- readFile path;
             lines <- map lineOrIo (lines file);
             print lines }
      In an equation for ‘printLines’:
          printLines path
            = do { file <- readFile path;
                   lines <- map lineOrIo (lines file);
                   print lines }
Compilation failed.

Sadly I am not yet very used to the error messages, so I don't understand
what ghci is telling me.
As far as I can tell (lines file) should give me an array of strings, which
I turn into an array of IO String.
Could that be the error? It shouldn't be [IO String] but instead IO
[String]? How do I turn the former into the latter?

Kind regards,
Jona

PS. It is not a school assignment, so I'll make sure to check out
System.Directory.


> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/19031ed5/attachment-0001.html>

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

Message: 4
Date: Thu, 29 Jun 2017 16:22:34 +0200
From: Silent Leaf <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] order of things in a file.hs?
Message-ID:
        <CAGFccjOHsJQKp7OcjKDj=4bn9uoybncjjyhgxze0pge+qtw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

hi,

what is your personal practice of ordering data/functions/main/hidden
functions, etc in a haskell file? like all trivial things they are very
hard to decide upon, given that either order of helpers then main function
/ value, or the other way round, has tempting pros and cons.

thanks in advance for your opinion!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/69cec4a5/attachment-0001.html>

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

Message: 5
Date: Thu, 29 Jun 2017 14:31:30 +0000
From: Norbert Melzer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] order of things in a file.hs?
Message-ID:
        <ca+bcvsv7mhrd18nzu6h2+-jrsji3aosrtnmyhhn0b5qamqx...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

In all languages that support arbitrary ordering I try to keep exported
stuff to the top and private stuff somewhere below.

Silent Leaf <[email protected]> schrieb am Do., 29. Juni 2017, 16:23:

> hi,
>
> what is your personal practice of ordering data/functions/main/hidden
> functions, etc in a haskell file? like all trivial things they are very
> hard to decide upon, given that either order of helpers then main function
> / value, or the other way round, has tempting pros and cons.
>
> thanks in advance for your opinion!
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/f6347c45/attachment-0001.html>

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

Message: 6
Date: Thu, 29 Jun 2017 16:47:32 +0200
From: Jona Ekenberg <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] order of things in a file.hs?
Message-ID:
        <CALvEEUem_s84=6A+ob=bkumcaqohc7vo9erjw3zw8xf_zbg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

2017-06-29 16:22 GMT+02:00 Silent Leaf <[email protected]>:

> hi,
>
> what is your personal practice of ordering data/functions/main/hidden
> functions, etc in a haskell file? like all trivial things they are very
> hard to decide upon, given that either order of helpers then main function
> / value, or the other way round, has tempting pros and cons.
>
> thanks in advance for your opinion!
>
>
I've started trying to write my program in a way that makes sense as when
reading it from top to bottom, like a story.
So if you write a function that calls upon other functions, those functions
can come immediately afterwards, if they're relevant, or if their names are
enough, they could be put in a different file altogether.
Some functions are bigger, like stories of their own. In those cases I
generally put them in a new file, like a new chapter. Those should be
coherent in themselves, and not depend on the first file in order to make
sense. If chapters are dependent on each other in that way, then maybe they
have to be rewritten.

This is a really course generalization, and I've actually not written code
like this for long. But I do think writing code in a way that makes sense
as a human reader is important, and humans tend to enjoy reading stories.
It doesn't really have to be some sort of magical journey (though it very
well could be!), but I think there should be some sort of red thread that
goes from one place in the code to the next, without extreme leaps that are
hard to understand.


> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170629/eb51e0f2/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 108, Issue 23
******************************************

Reply via email to