Thread: Can you count to ten?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by MacNilly View Post
    Code:
    main = foldr((>>) . putStrLn . show)(return ())([1 .. 10]);
    Thats the most ugly code I've ever seen. If I didn't know it prints 1 to 10 then I'd be lost in days of analysis trying to figure it out. Sucks. Whats wrong with just

    Code:
    [1 .. 10]
    Also WTF is the return() HAH who ever writes this crap?
    4. Weeks.

    This took me 4 weeks to figure out lol XD

    It is very ugly but let me explain, the foldr signature is :
    Code:
    foldr :: (a -> b -> b) -> b -> [a] -> b
    I'm using `[a]` instead of traversable `t a` for simplicity.

    foldr is somewhat intuitive in that it folds from right to left. Using this and the above type signature, we can infer that first part of our fold is
    Code:
    (>>) . putStrLn . show $ 10
    which gives us a partially applied monadic sequence operation. Using the `b` part of our signature, this is then applied to `return ()` which just gives us an empty monad instance.

    So basically, what we wind up with is this:
    Code:
    ... (8 >> (9 >> (10 >> IO())))
    Remember that
    Code:
    m >> k = m >>= \_ -> k
    for monads.

    So you wind up with (let's pretend we don't need the show crap and we're only going 1 through 5):
    Code:
    putStrLn("1") >> (putStrLn("2") >> (putStrLn("3") >> (putStrLn("4") >> ((putStrLn("5") >> return ())))))
    Long-story short, this is why Haskell is stupid and isn't used in the industry and no amount of hipster nonsense (credit to Yarin for the hipster term ;P) can ever hope to make it a practical and viable programming language.

    However, learning category theory has done nothing but make me a better programmer.

    Edit:

    Some of the stuff is just there because Haskell has a decent type system. If we have a number, we need to Show it first and then when we putStrLn, we simply get a monad back out. Because we want to sort of chain these guys together, it's easiest to fold using a sequence which does our >>= stuff for us. Basically, we want to map over the list creating an IO instance of each element and then we want to chain them together. This is most easily expressed as a fold with a monadic sequencing.

    Something something functors something something applicative, no one understands my code, god, Mom!
    Last edited by MutantJohn; 11-29-2016 at 10:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 08:16 AM
  2. Even I Can Count Better Than That...
    By pianorain in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-02-2005, 07:17 PM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread