Thread: Can you count to ten?

  1. #31
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    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?
    Last edited by MacNilly; 11-28-2016 at 07:17 AM.

  2. #32
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    BBC Basic

    Code:
    REM 1-10 printing
    REM written by Ada Skyla-Rose
    REM ===================
    FOR i = 1 TO 10
    PRINT i
    NEXT i
    ENDPROC
    END
    Double Helix STL

  3. #33
    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.

  4. #34
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You would make a good Java programmer, John

  5. #35
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    What makes you say that?

  6. #36
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Your Haskell is enterprise quality

  7. #37
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Lol I literally just started reading about how in categorical terms an applicative is actually a monoidal functor. I think everything in Haskell is enterprise-quality by nature of the beast XD

    Seriously, the most useless thing ever but I can't pull myself away from it!

  8. #38
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,445
    Well, in procedural terms an applicative can be thought of as a function. So, you could say C++ is enterprise-quality.
    The good thing about comparison theorems is that you can always think of an ant as type of elephant.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #39
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Heh heh. Not as much fun as the category theory version though :P

    It's really fun to think about while I'm at work because Lord knows I can't focus on actual work that long.

  10. #40
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    Quote Originally Posted by MutantJohn View Post
    However, learning category theory has done nothing but make me a better programmer.
    Well there you go then. You found a use for Haskell after all
    Double Helix STL

  11. #41
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Well, I can see you "learned you some Haskell for some good". Bravo!

    I mean, I like a lot of the concepts of Haskell, but when I hit monads I gave up. I understand that they adopted the monad concept in order to encapsulate side-effects in order to make Haskell "pure", but to me it just seems to be to be excessively difficult to apply and a basically useless abstraction. I mean, think about it: some side effect must have occurred, if any I/O is actually done. Then, the whole point of the monad type is to limit the amount of "damage" that these side effects have on the rest of the program. However, this could be done in other ways that are conceptually simpler. What about a language that had an "iterative" and "functional" subsets... and differentiated between "pure" functions and "side-effecting" functions, and these could only be used in certain contexts? I think it would achieve the same separation between pure/impure that is the whole point of the monad, without introducing the difficulties.

    For example, you could have "functions" (pure) and "procedures" (impure), denoted by different keywords. While the majority of the program would be pure (disallowing "impure" procedure calls) you could have a "do" block that enters a iterative context, where you can freely mix assignment and procedure call. Actually, I am working on a language like this at the moment...
    Last edited by MacNilly; 12-02-2016 at 01:29 AM.

  12. #42
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    D actually supports a "pure" keyword. I wish C++ did too... :P

    But yeah, the IO monad is basically a way of guaranteeing the same function execution, even if the input is different. Fundamentally, FP is about categories over types and how those types are transformed from one to the other. The type system does help keep things in line but that's more or less because it represents a functor and how functors transform categories.

  13. #43
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by MutantJohn View Post
    D actually supports a "pure" keyword. I wish C++ did too... :P
    It's not as useful as it seems. Purity is trivial to statically prove. And it doesn't help with CTFE, since if you want to guarantee it, the expression in question should be decorated as such, which C++ does have, in the form of constexpr.

  14. #44
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Apl:
    Code:
    ⍳10

  15. #45
    Registered User
    Join Date
    Oct 2016
    Location
    Wales, UK
    Posts
    12
    Quote Originally Posted by swgh View Post
    BBC Basic

    Code:
    REM 1-10 printing
    REM written by Ada Skyla-Rose
    REM ===================
    FOR i = 1 TO 10
    PRINT i
    NEXT i
    ENDPROC
    END
    This won't run in BBC BASIC since you haven't defined the procedure. Remove the ENDPROC and it will work - or define the procedure. This will work in BBC BASIC for Windows:

    Code:
          PROC_loop
          PRINT
          PRINT "THE END."
          END
         
          DEF PROC_loop
          FOR I=1 TO 10
            PRINT I;
          NEXT I
          ENDPROC
    ...as will the REPEAT...UNTIL loop in BBC BASIC that I posted earlier on in this thread.

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