Hacker News new | ask | show | jobs
by eutectic 3883 days ago
And in Haskell (feel free to include this):

    import Control.Concurrent (threadDelay)                                         
    import System.IO

    main :: IO ()                                                                   
    main = withFile "/sys/class/leds/beaglebone:green:usr0/brightness" AppendMode go
      where put h s = hPutStrLn h s >> threadDelay 2000000                          
            go  h   = let m = put h "0" >> put h "1" >> m in m
3 comments

As always, prefer prebuilt recursion scheme combinators instead of making your own. The following, using `forever`, is IMO much more readable because you don't have to mentally resolve the recursion:

  main :: IO ()
  main = withFile ledFile AppendMode $ \h -> do
      let put s = hPutStrLn h s >> threadDelay 2000000                          
      forever $ do
         put "0" 
         put "1"
    where
      ledFile = "/sys/class/leds/beaglebone:green:usr0/brightness"
Why:

    go  h = let m = put h "0" >> put h "1" >> m in m
Rather than:

    go  h = put h "0" >> put h "1"
Oh, recursion...
Thanks, any thoughts on why I get this?

$ ghc -o blink blink.hs [1 of 1] Compiling Main ( blink.hs, blink.o )

blink.hs:7:22: parse error on input `='

Not sure; I copy-pasted it back from hackernews and it works fine for me.