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. Re:  hanoi 4 problem
      (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   2. Re:  function to read file and return list of contents not
      compiling (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   3. Re:  hanoi 4 problem (Roelof Wobben)
   4. Re:  function to read file and return list of contents not
      compiling (Geoffrey Bays)
   5. Re:  tower hanoi problem (Doug McIlroy)


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

Message: 1
Date: Thu, 19 Feb 2015 22:57:54 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] hanoi 4 problem
Message-ID:
        <cajbew8n4wosupcehj4hh74htyfovepk2hpgxpaeffckiphe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

What you did recursively is ...

   - Move (n - 1) from peg1 to peg3
   - Move 1 from peg1 to peg3
   - Move (n - 1) from peg1 to peg3

.. which is not what you want. Keep thinking, you will get it.
Some things to consider:

   - Distribute the discs on two intermediates.
   - Keep track of the order in which discs are assembled on the
   intermediates.
   - Move 1 disc from peg1 to peg4.
   - Then, move the distributed discs back in the reverse order from how
   you put them there.

By keeping track, I mean keep track of which peg is filled first.
Hope this helps.

On 19 February 2015 at 19:20, Roelof Wobben <[email protected]> wrote:

> Hello,
>
> I try now to make the optional exercise hanoi with 4 pegs,
>
> So I did on paper this :
>
> start               temp1               temp2                  end
>
> 3
> 2
> 1
>
> first move  :  start -> temp2
>
> start               temp1           temp2 end
> 2                                              3
> 1
>
> second move : start -> temp1
>
> start                  temp1       temp2                      end
> 1                        2                  3
>
> now I tried to make this piece in code
>
> So I did :
>
> hanoi4 1 start _  _ end = "move 1 disk from " ++ [start] ++ " to " ++
> [end] ++ ".\n"
> hanoi4 n start temp1 temp2 end = hanoi4 (n-1) start end temp1 temp2 ++
> hanoi4 1 start end temp1 temp2 ++ hanoi4 (n-1) start  temp1 end temp2
>
> because on the first step   start must be  start and  end must be temp1
> and on the second step start must be start and end must be temp2
>
> but when I run in I see this output :
>
> move 1 disk from a to b.
> move 1 disk from a to b.
> move 1 disk from a to b.
> move 1 disk from a to c.
> move 1 disk from a to d.
> move 1 disk from a to d.
> move 1 disk from a to d.
>
> Can anyone explain or let me see where my thinking took the wrong turn ?
>
> Roelof
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150219/dc6e79b6/attachment-0001.html>

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

Message: 2
Date: Thu, 19 Feb 2015 22:59:00 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function to read file and return list
        of contents not compiling
Message-ID:
        <cajbew8ov_zeaa6fruf1twdtpgatf9rhndkrvjxq02huzuzn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thus,

    return (lines contents)

will work.

On 19 February 2015 at 22:55, Sumit Sahrawat, Maths & Computing, IIT (BHU) <
[email protected]> wrote:

> The type should be IO [String]. You do IO, and it results in a list of
> Strings.
> Also, take a look at the following types:
>
>     contents :: String
>     lines :: String -> [String]
>
> Which means that,
>
> lines contents :: [String]
>
> But because you're dealing with a monad (IO in this case), this will not
> typecheck. You can convert a pure value to a monadic value using the return
> function:
>
>     return :: Monad m => a -> m a
>
> Not specific to IO, but all monads.
> Hope this helps.
>
> On 19 February 2015 at 22:47, Geoffrey Bays <[email protected]> wrote:
>
>> Haskellers:
>>
>> I want to write a function that takes a class name and reads from a file
>> and then returns the
>> list of String of the file lines. I have experimented with various return
>> types, but GHC does not like any of them. Here is my function:
>>
>> readData :: String -> IO String   -- what type here??
>> readData classNameP = do
>>     let fileName = classNameP ++ ".txt"
>>     contents <- readFile fileName
>>     lines contents
>>
>> Not to complain, but this would not be difficult in any other language I
>> have tried, so I could use
>> some explanation. I suspect it has to do with understanding the IO Monad.
>>
>> Many Thanks
>>
>> Geoffrey
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150219/4808f0d9/attachment-0001.html>

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

Message: 3
Date: Thu, 19 Feb 2015 18:30:53 +0100
From: Roelof Wobben <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing
        List - Discussion of primarily beginner-level topics related to
        Haskell <[email protected]>
Subject: Re: [Haskell-beginners] hanoi 4 problem
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150219/5bc365db/attachment-0001.html>

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

Message: 4
Date: Thu, 19 Feb 2015 13:00:08 -0500
From: Geoffrey Bays <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing
        List - Discussion of primarily beginner-level topics related to
        Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function to read file and return list
        of contents not compiling
Message-ID:
        <cad8ukx6wjqcbpo-o2e32ven3dnmhasjf7yo_tungx1hczh4...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thanks. I played around some more with return and came up with this which
compiles fine:

readData :: String -> IO [String]
readData classNameP = do
    let fileName = classNameP ++ ".txt"
    contents <- readFile fileName
    return $ lines contents

Geoffrey

On Thu, Feb 19, 2015 at 12:25 PM, Sumit Sahrawat, Maths & Computing, IIT
(BHU) <[email protected]> wrote:

> The type should be IO [String]. You do IO, and it results in a list of
> Strings.
> Also, take a look at the following types:
>
>     contents :: String
>     lines :: String -> [String]
>
> Which means that,
>
> lines contents :: [String]
>
> But because you're dealing with a monad (IO in this case), this will not
> typecheck. You can convert a pure value to a monadic value using the return
> function:
>
>     return :: Monad m => a -> m a
>
> Not specific to IO, but all monads.
> Hope this helps.
>
> On 19 February 2015 at 22:47, Geoffrey Bays <[email protected]> wrote:
>
>> Haskellers:
>>
>> I want to write a function that takes a class name and reads from a file
>> and then returns the
>> list of String of the file lines. I have experimented with various return
>> types, but GHC does not like any of them. Here is my function:
>>
>> readData :: String -> IO String   -- what type here??
>> readData classNameP = do
>>     let fileName = classNameP ++ ".txt"
>>     contents <- readFile fileName
>>     lines contents
>>
>> Not to complain, but this would not be difficult in any other language I
>> have tried, so I could use
>> some explanation. I suspect it has to do with understanding the IO Monad.
>>
>> Many Thanks
>>
>> Geoffrey
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
> _______________________________________________
> 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/20150219/ab4b61d1/attachment-0001.html>

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

Message: 5
Date: Thu, 19 Feb 2015 21:27:10 -0500
From: Doug McIlroy <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] tower hanoi problem
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On 2/19/15 4:47 AM, Joel Neely wrote:

> Incidentally, I also find it interesting to see the subtle effects
> that our terminology has on the  way we approach problems. Thinking of
> a list as "it may be empty or not" takes my thoughts in a different
> direction than if I think "it may have a head or not".

A telling aphorism. It will stick with me for a long time.

Regarding the problem that sparked this discussion, Hanoi: if you 
think first about the base case, it's likely to be this: to transfer
a oone-disc pile from A to B using C as an intermediate, move A to B.
If you think first about the recursive step, you'll likely come
up with a different base case: to transfer zero discs, do nothing. 
With this insight, you've generalized your original understanding 
of the problem.

Doug


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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 80, Issue 56
*****************************************

Reply via email to