[Haskell-beginners] How best to do this?

David McBride toad3k at gmail.com
Mon Apr 29 17:37:47 CEST 2013


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 <objitsu at gmail.com> 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 <byorgey at seas.upenn.edu> 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
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list