[Haskell-beginners] How best to do this?

emacstheviking objitsu at gmail.com
Mon Apr 29 16:37:10 CEST 2013


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.



On 29 April 2013 15:07, Benjamin Edwards <edwards.benj at gmail.com> wrote:

> forM_ [0..3] \b -> stepBit b .... ?
> On 29 Apr 2013 15:01, "emacstheviking" <objitsu at gmail.com> 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
>>
>>
>> It feels a little smelly... if I don't assign the result of the list
>> comprehension to "x" the return type blows at me hence the return(), to get
>> past the door steward type checker... so what would be the "ideal" way to
>> just execute my LC to step the bits and then return.
>>
>> I thought of using "liftIO" but I think that would be "backwards" here if
>> you catch my drift. I am already in the IO monad as it is, so that has
>> confused me because liftIO lifts something "into" the IO monad and I am
>> already there... this project is making me work harder at my Haskell and I
>> think that once I've completed it I will be much further advanced with it...
>>
>> So, if anybody cares to suggest more elegant and Haskell like ways to
>> re-factor the above code I am more than happy to know about it!
>>
>> All the best,
>> Sean.
>>
>>
>>
>> _______________________________________________
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130429/5d7925f0/attachment-0001.htm>


More information about the Beginners mailing list