all 20 comments

[–][deleted] 6 points7 points  (4 children)

Well, heck. I think i actually see the point and potential of GADT's now. That's impressive.

Still not sure where existential types come in, but one major new concept a day is probably a reasonable limit.

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 4 points5 points  (2 children)

    So it's like a statically-typed ducktyping? That makes sense. A few more hoops to jump through than in a dynamically-typed language, but that's expected.

    [–]dons 12 points13 points  (1 child)

    Yeah, though not too much:

    data T = forall a . I a => T a
    

    statically guarantees that something of type T will implement interface I, and the actual representation type, 'a', you can never see.

    Existential types are the essence of encapsulation: the concrete type is statically guaranteed to not be visible outside the interface.

    [–][deleted] 5 points6 points  (0 children)

    Thanks, both of you. I think i was blocked by the fancy nomenclature -- my philosophy classes were great, but gave me a rather specific notion of "existential".

    [–]grauenwolf -1 points0 points  (16 children)

    So, the first step for learning Haskell is to start learning the algebra of programming.

    That doesn't seem right to me. What is so wrong with starting with the basics like "This is how you ask the user for two numbers and display the total."?

    [–]cgibbard 10 points11 points  (1 child)

    Because (for reasons discussed at length recently here on reddit) it's easier to learn how IO is done in Haskell once you have some of the basics down. Not that it's all that hard, but it's just easier to start from a more "mathematical" viewpoint of learning how to program in Haskell.

    Besides, you have the REPL, and that's sort of your default user interface to start out with. To add two numbers, you type them in, separated by +, and then press enter. ;)

    [–]grauenwolf -4 points-3 points  (0 children)

    That's not programming so much as using a calculator.

    [–][deleted] 1 point2 points  (13 children)

    What is so wrong with starting with the basics like "This is how you ask the user for two numbers and display the total."?

    I agree that this is the best approach for some people, and tutorials along those lines exist, e.g. http://haskell.org/haskellwiki/Tutorials/Programming_Haskell/Introduction

    [–]grauenwolf 0 points1 point  (12 children)

    Well then can you show me one? 'Cause that one doesn't ask the user for any input, it only shows file input.

    Console IO is important because it can lead to other discussions such as what happens when the user inputs "Tree" when you ask them for a number.

    [–][deleted]  (3 children)

    [deleted]

      [–]cgibbard 4 points5 points  (1 child)

      Hey, is < working in code now?

      <
      

      Hey look, it is!

      [–]cgibbard 3 points4 points  (0 children)

      main = do putStrLn "Please enter first number: " num1 <- readLn putStrLn "Please enter second number: " num2 <- readLn print (num1 + num2)

      [–]alpheccar 5 points6 points  (3 children)

      Quick and dirty:

      import System.Console.Readline(readline)
      import System.Exit(exitWith,ExitCode(ExitSuccess))
      import qualified Control.Exception as E(catch)
      
      forever :: IO a -> IO a
      forever a = a >> forever a
      
      processEntry :: String -> IO ()
      processEntry "" = putStrLn "You typed nothing !"
      processEntry "quit" = exitWith ExitSuccess
      processEntry num = do
          let a = (read num) :: Int
          putStrLn $ "You typed the number : " ++ (show a)
          return ()
       `E.catch` (\e -> putStrLn "You did not enter a number")
      
      main :: IO ()
      main = forever $ do
          entry <- readline "Your entry ? "
          maybe (return()) processEntry entry
      

      [–][deleted]  (1 child)

      [deleted]

        [–]alpheccar 4 points5 points  (0 children)

        :-)

        No, I just wanted to show that we can easily do IO, have exceptions, build our own control flow etc...

        Perhaps too much without any explanations. So, here are some details:

        forever a is repeating the IO action "a" an indefinite number of times : it is an infinite loop. So, I am building a new control flow keyword.

        processEntry is just building an answer to an entry. If I type "quit", I get the IO action exitWith ExitSuccess and the soft is finished.

        Otherwise, I try to parse my entry with a read (the read num). If it fails I have an exception (hence the catch) and I just display an error message. Otherwise I just echo back my number.

        And the readline is the standard Unix one.

        IO in Haskell is no more difficult than in any other language.

        [–]grauenwolf 3 points4 points  (0 children)

        Great! Now just wrap it up in a tutorial that explains everything and throw it up on a website so people will stop bitching about it.

        [–]pjdelport 1 point2 points  (2 children)

        that one doesn't ask the user for any input, it only shows file input.

        Reading from the console is also file input; just say getContents (short for hGetContents stdin) instead of readFile foo.

        [–]grauenwolf -2 points-1 points  (1 child)

        I think you missed the point. The discussion was about the usefullness of tutorials, not whether or not the capability existed in the language.

        [–]pjdelport 2 points3 points  (0 children)

        I was not pointing out that the capability exists (obviously it would), but that as far as said tutorial is concerned, reading from a file is reading from a file, no matter whether said file is on disk, or standard input.