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. Re:  How best to do this? (Mateusz Kowalczyk)
   2. Re:  How best to do this? (Brent Yorgey)
   3. Re:  How best to do this? (emacstheviking)
   4. Re:  How best to do this? (David McBride)
   5. Re:  How best to do this? (emacstheviking)
   6. Re:  How best to do this? (David McBride)


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

Message: 1
Date: Mon, 29 Apr 2013 15:54:26 +0100
From: Mateusz Kowalczyk <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 29/04/13 15:37, emacstheviking wrote:
> Benjamin, I have managed to reduce it all to this for which I thank
> you, it feels much neater now...
> 
> doOption dev (Forward n)  = do putStrLn $ "> STEP FORWARD " ++
> (show n) forM_ [3..0] (\b -> stepBit dev ioPORTA b)
> 
> doOption dev (Backward n) = do putStrLn $ "> STEP BACKWARD " ++
> (show n) forM_ [0..3] (\b -> stepBit dev ioPORTA b)
> 
> stepBit :: HWHandle -> Word8 -> Word8 -> IO () stepBit dev p b =
> setBit p b 0 >> setBit p b 1 where setBit p b s = HW.setPortBit dev
> p b s >> stepDelay
> 
> 
> I think I will also try to use "forEach_" as well which means I can
> then compose is something that passes in [0..3] or [3..0] to the
> composition... I may even learn something in the process! God I
> love the way that Haskell makes you *want* to reduce things to
> their most succint form, even if it hurts along the way. Only Lisp
> ever (ha, still does!) made me feel like Haskell does, aka a total
> n00b after 28 years in the business! LMAO
> 
> Thanks list.
> 

The next step would be to factor out that horrible repetition. Maybe
something like this:

doOption dev x = case x of
  (Backward n) -> stepper "FORWARD"
  (Forward n) -> stepper "BACKWARD"
  where stepper w = do
          putStrLn $ "> STEP " ++ w ++ (show n)
          forM_ [0 .. 3] (\b -> stepBit dev ioPORTA b)

I think this should work. It's also easier to introduce more options
this way.

- -- 
Mateusz K.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iQIcBAEBAgAGBQJRfomiAAoJEM1mucMq2pqXpc8P/1X+snuFWZkprraX0UAZ95e7
pvCGelnxxW2rN1iUolzM5rD6MYBKTBhEMBp+SALrdQ3xUIYk/EP1yRfKnxJITe1g
CN08XqUcX47QMtmxQPvngKITT1L+voAL0DSei225ohT/lMIwetfbJuXub9iKzZ/G
YwLenR3R2E6iznys/Z/93Kk9tP5fXrOEkAFurqhe+xYTQnvDTMClv9cYEAj4xB1f
6x9gfqt5MbvJSyJ4PrGkJtXmS4RGSrysR5Eccf2p9CETlbBEo3uBHpzAPmqGXDFs
nfeuBoWxMb/X1w9ABim/i0pITR25S+XHG9HAlQNZ3jikY057Ict9YIr7xYkFjsPh
PEwVPlbf3QOTJW3IBXMbVTjA7yPiJnjG/tgAB3b8NYQnVfgPAQ/g7bw77pfm9W6X
ajbXgQ9slPADhG4vEKr7ktT8CD+DlOcm/iBYockQe3h6RoFSsRFSSoCOCM8ogPUl
xWp7wlZeRnP4AGB+R5vSHd06Fzk60L7D7vJvgTLINjqvhWGNrcVhvkEsCm9MWXEM
/rdqnbPBuUM3qz0MQnxWHvLeXgAIP5SzYxv2FthXlF+00/1Emqr1vk2F27mppDfZ
Xy/PUDjYTSHLCgPNvHVLyE9xfyVAPyqM2TarBrn8jyvi5Bx7WDn++4POD5B39MvM
TD57PLQJLg0oVrrLFnYQ
=Mbcq
-----END PGP SIGNATURE-----



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

Message: 2
Date: Mon, 29 Apr 2013 10:56:43 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, Apr 29, 2013 at 02:59:29PM +0100, emacstheviking wrote:
> I have built a library for using the Hexwax expandIO-USB chip and I have
> now got some code to drive a stepper motor:
> 
> doOption :: HWHandle -> Flag -> IO ()
> doOption dev (Backward n) = do
>   putStrLn $ "> STEP BACKWARD " ++ (show n)
>   let x = [ stepBit b | b <- [3..0]]
>   return ()
>     where
>       stepBit p b = setBit p b 0 >> setBit p b 1
>         where setBit p b s = HW.setPortBit dev p b s >> stepDelay

The other posted solutions are good, but I also want to make a very
important comment about the above code: it does not actually step any
bits!  All it does is print some stuff.  x is simply a name for a list
of IO actions; it is never used so it just gets garbage collected and
the IO actions are never run.

-Brent



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

Message: 3
Date: Mon, 29 Apr 2013 16:26:43 +0100
From: emacstheviking <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAEiEuUKV=ejh3coelgnmihaycmaurljf835dgdhnre_+xae...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

damn that lazy evaluation! LMAO  ...a good point brent and yuo have no
doubt saved me hours of head scratching this evening when I try out the
"new improved software". Oh dear oh dear oh dear...

doOption dev (Forward n)  = do
  putStrLn $ "> STEP FORWARD " ++ (show n)
  stepBits dev ioPORTA [3..0]

doOption dev (Backward n) = do
  putStrLn $ "> STEP BACKWARD " ++ (show n)
  stepBits dev ioPORTA [0..3]

stepBits dev port = mapM_ stepIt
  where stepIt bit = mapM_ (\s -> HW.setPortBit dev port bit s >>
stepDelay) [0,1]

I now have the above as my current "final" implementation... hopefully that
*does* do what I think it does because mapM_ is driving it and will cause
evaluation of the actions?



On 29 April 2013 15:56, Brent Yorgey <[email protected]> wrote:

> On Mon, Apr 29, 2013 at 02:59:29PM +0100, emacstheviking wrote:
> > I have built a library for using the Hexwax expandIO-USB chip and I have
> > now got some code to drive a stepper motor:
> >
> > doOption :: HWHandle -> Flag -> IO ()
> > doOption dev (Backward n) = do
> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
> >   let x = [ stepBit b | b <- [3..0]]
> >   return ()
> >     where
> >       stepBit p b = setBit p b 0 >> setBit p b 1
> >         where setBit p b s = HW.setPortBit dev p b s >> stepDelay
>
> The other posted solutions are good, but I also want to make a very
> important comment about the above code: it does not actually step any
> bits!  All it does is print some stuff.  x is simply a name for a list
> of IO actions; it is never used so it just gets garbage collected and
> the IO actions are never run.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130429/551be455/attachment-0001.htm>

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

Message: 4
Date: Mon, 29 Apr 2013 11:37:47 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <can+tr40mtwd7greervs+f_hdnmtgu+ilzfgpr+uafqxe0zg...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

One other gotcha.  I don't know why it is this way, but [3..0]
evaluates to [].  I have no idea why reverse notation is not allowed.
But you can just manually reverse it or you can go [3,2..0].

On Mon, Apr 29, 2013 at 11:26 AM, emacstheviking <[email protected]> wrote:
> damn that lazy evaluation! LMAO  ...a good point brent and yuo have no doubt
> saved me hours of head scratching this evening when I try out the "new
> improved software". Oh dear oh dear oh dear...
>
> doOption dev (Forward n)  = do
>   putStrLn $ "> STEP FORWARD " ++ (show n)
>   stepBits dev ioPORTA [3..0]
>
>
> doOption dev (Backward n) = do
>   putStrLn $ "> STEP BACKWARD " ++ (show n)
>   stepBits dev ioPORTA [0..3]
>
> stepBits dev port = mapM_ stepIt
>   where stepIt bit = mapM_ (\s -> HW.setPortBit dev port bit s >> stepDelay)
> [0,1]
>
> I now have the above as my current "final" implementation... hopefully that
> *does* do what I think it does because mapM_ is driving it and will cause
> evaluation of the actions?
>
>
>
>
> On 29 April 2013 15:56, Brent Yorgey <[email protected]> wrote:
>>
>> On Mon, Apr 29, 2013 at 02:59:29PM +0100, emacstheviking wrote:
>> > I have built a library for using the Hexwax expandIO-USB chip and I have
>> > now got some code to drive a stepper motor:
>> >
>> > doOption :: HWHandle -> Flag -> IO ()
>> > doOption dev (Backward n) = do
>> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
>> >   let x = [ stepBit b | b <- [3..0]]
>> >   return ()
>> >     where
>> >       stepBit p b = setBit p b 0 >> setBit p b 1
>> >         where setBit p b s = HW.setPortBit dev p b s >> stepDelay
>>
>> The other posted solutions are good, but I also want to make a very
>> important comment about the above code: it does not actually step any
>> bits!  All it does is print some stuff.  x is simply a name for a list
>> of IO actions; it is never used so it just gets garbage collected and
>> the IO actions are never run.
>>
>> -Brent
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 5
Date: Mon, 29 Apr 2013 16:44:44 +0100
From: emacstheviking <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <caeieuu+kis2ztbmbufhjk0jwxlgpwsdphijaaicrpvvdjwa...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

ROFLCOPTER indeed Batman!

I had no idea of that... I just *assumed* (usual rules apply I guess) that
[3..0] was the "opposite" of [0..3] but sure enough a wuicj ghci session
reveals the bitter truth!

Thanks again... i can see that it's not just me that is too lazy at times.
I guess writing [3,2..0] will do for now but is that a bug or is there some
other reasoning behind it?

We live and learn, well, I live anyway...

:)


On 29 April 2013 16:37, David McBride <[email protected]> wrote:

> One other gotcha.  I don't know why it is this way, but [3..0]
> evaluates to [].  I have no idea why reverse notation is not allowed.
> But you can just manually reverse it or you can go [3,2..0].
>
> On Mon, Apr 29, 2013 at 11:26 AM, emacstheviking <[email protected]>
> wrote:
> > damn that lazy evaluation! LMAO  ...a good point brent and yuo have no
> doubt
> > saved me hours of head scratching this evening when I try out the "new
> > improved software". Oh dear oh dear oh dear...
> >
> > doOption dev (Forward n)  = do
> >   putStrLn $ "> STEP FORWARD " ++ (show n)
> >   stepBits dev ioPORTA [3..0]
> >
> >
> > doOption dev (Backward n) = do
> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
> >   stepBits dev ioPORTA [0..3]
> >
> > stepBits dev port = mapM_ stepIt
> >   where stepIt bit = mapM_ (\s -> HW.setPortBit dev port bit s >>
> stepDelay)
> > [0,1]
> >
> > I now have the above as my current "final" implementation... hopefully
> that
> > *does* do what I think it does because mapM_ is driving it and will cause
> > evaluation of the actions?
> >
> >
> >
> >
> > On 29 April 2013 15:56, Brent Yorgey <[email protected]> wrote:
> >>
> >> On Mon, Apr 29, 2013 at 02:59:29PM +0100, emacstheviking wrote:
> >> > I have built a library for using the Hexwax expandIO-USB chip and I
> have
> >> > now got some code to drive a stepper motor:
> >> >
> >> > doOption :: HWHandle -> Flag -> IO ()
> >> > doOption dev (Backward n) = do
> >> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
> >> >   let x = [ stepBit b | b <- [3..0]]
> >> >   return ()
> >> >     where
> >> >       stepBit p b = setBit p b 0 >> setBit p b 1
> >> >         where setBit p b s = HW.setPortBit dev p b s >> stepDelay
> >>
> >> The other posted solutions are good, but I also want to make a very
> >> important comment about the above code: it does not actually step any
> >> bits!  All it does is print some stuff.  x is simply a name for a list
> >> of IO actions; it is never used so it just gets garbage collected and
> >> the IO actions are never run.
> >>
> >> -Brent
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130429/34c998f0/attachment-0001.htm>

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

Message: 6
Date: Mon, 29 Apr 2013 11:57:38 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] How best to do this?
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <can+tr40dn+iko+np9tj4sq3zq-00b119qssh2x-pkk+roqn...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I think it is because that syntax is desugared directly to enumFromTo
x y and enumFromTo 3 0 is [].  Random things would probably blow up if
you make that function work in reverse.  But I would love it if ghc
just checked whether the first was lower than the second and swapped
them as a convenience.

On Mon, Apr 29, 2013 at 11:44 AM, emacstheviking <[email protected]> wrote:
> ROFLCOPTER indeed Batman!
>
> I had no idea of that... I just *assumed* (usual rules apply I guess) that
> [3..0] was the "opposite" of [0..3] but sure enough a wuicj ghci session
> reveals the bitter truth!
>
> Thanks again... i can see that it's not just me that is too lazy at times. I
> guess writing [3,2..0] will do for now but is that a bug or is there some
> other reasoning behind it?
>
> We live and learn, well, I live anyway...
>
> :)
>
>
>
> On 29 April 2013 16:37, David McBride <[email protected]> wrote:
>>
>> One other gotcha.  I don't know why it is this way, but [3..0]
>> evaluates to [].  I have no idea why reverse notation is not allowed.
>> But you can just manually reverse it or you can go [3,2..0].
>>
>> On Mon, Apr 29, 2013 at 11:26 AM, emacstheviking <[email protected]>
>> wrote:
>> > damn that lazy evaluation! LMAO  ...a good point brent and yuo have no
>> > doubt
>> > saved me hours of head scratching this evening when I try out the "new
>> > improved software". Oh dear oh dear oh dear...
>> >
>> > doOption dev (Forward n)  = do
>> >   putStrLn $ "> STEP FORWARD " ++ (show n)
>> >   stepBits dev ioPORTA [3..0]
>> >
>> >
>> > doOption dev (Backward n) = do
>> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
>> >   stepBits dev ioPORTA [0..3]
>> >
>> > stepBits dev port = mapM_ stepIt
>> >   where stepIt bit = mapM_ (\s -> HW.setPortBit dev port bit s >>
>> > stepDelay)
>> > [0,1]
>> >
>> > I now have the above as my current "final" implementation... hopefully
>> > that
>> > *does* do what I think it does because mapM_ is driving it and will
>> > cause
>> > evaluation of the actions?
>> >
>> >
>> >
>> >
>> > On 29 April 2013 15:56, Brent Yorgey <[email protected]> wrote:
>> >>
>> >> On Mon, Apr 29, 2013 at 02:59:29PM +0100, emacstheviking wrote:
>> >> > I have built a library for using the Hexwax expandIO-USB chip and I
>> >> > have
>> >> > now got some code to drive a stepper motor:
>> >> >
>> >> > doOption :: HWHandle -> Flag -> IO ()
>> >> > doOption dev (Backward n) = do
>> >> >   putStrLn $ "> STEP BACKWARD " ++ (show n)
>> >> >   let x = [ stepBit b | b <- [3..0]]
>> >> >   return ()
>> >> >     where
>> >> >       stepBit p b = setBit p b 0 >> setBit p b 1
>> >> >         where setBit p b s = HW.setPortBit dev p b s >> stepDelay
>> >>
>> >> The other posted solutions are good, but I also want to make a very
>> >> important comment about the above code: it does not actually step any
>> >> bits!  All it does is print some stuff.  x is simply a name for a list
>> >> of IO actions; it is never used so it just gets garbage collected and
>> >> the IO actions are never run.
>> >>
>> >> -Brent
>> >>
>> >> _______________________________________________
>> >> Beginners mailing list
>> >> [email protected]
>> >> http://www.haskell.org/mailman/listinfo/beginners
>> >
>> >
>> >
>> > _______________________________________________
>> > Beginners mailing list
>> > [email protected]
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 58, Issue 49
*****************************************

Reply via email to