in reply to Re^11: World's shortest intro to function programming
in thread Thread on Joel on software forum : "I hate Perl programmers."

The real essence and subtlety of FP is the typing system
Ah, ha. Now we're getting somewhere. I could definitely see where one could go astray, if they start following the reasearch guys too closely. They *love* proposing new additions to the type system to see what results (that's their job after all). Here's where I'll heartily recommend the book The Haskell School of Expression: Learning Functional Programming through Multimedia. I found I needed an actual text to use in my journey into FP. While PDF's might be good enough to learn a new language if it is sufficiently close to one I already knew, I found learning FP from PDF's to be mostly unworkable. "The School of Expression" book covers the Haskell-98 standard, which ignores the bleeding edge type system extensions (this is a good thing). Also, as a side note, I'm obligated to mention that while static typing might be used in the more popular FP languages like Haskell and OCaml, it is by no means the essence of FP. You can do FP in Scheme and Perl if you so desire (I'll also mention my new favorite untyped functional language,Joy, although it is pretty much completely impractical at the moment).
Uses syntax in the examples that you can't simply type into the "runloop interfaces" without modifying it.
The Haskell interpreter prompt is inside an implicit "do" block (which is what sequences IO actions in a lazy language like Haskell). I could see where that could be confusing. What that basically means is that you have to use the 'let' keyword to introduce a new definition. (Not to be confused with "let .. in" construct you use in regular code).
What I mean by "2D syntax" the Python-style required whitespace--2d alignment rules.
That's probably just one of those "matters-of-taste" things. Some folks like curley braces, others don't.
And what is 'a? Where did that come from? "a" seems to be a "generic type placeholder" generated by the type inferencing, but what is the ' doing? Is it a function that tests the value of the type represented by the "a"?
I know nothing about Ocaml, but I'll take a stab at it anyway. In Haskell, there's nothing too special about the single quote, so you can use them as part of your variable names. When used that way, it is pronounced "prime". Also in Haskell, use of lower case names in a type signature indicate a type variable (the type signature shows what the types of the arguments and return value are for a function). A type variable can represent any type, as opposed to a specific type instance like Integer, Float, String, etc. (note the capitalization). For example, the type of "map" is...
map :: (a -> b) -> [a] -> [b]
...which means that "map" takes two arguments (ignore currying for now). The first argument (a -> b) is a function which takes elements of type "a" and returns elements of type "b". The second argument ([a]) that "map" takes is a list of elements of type "a". And the return type is a list of elements of type "b". For instance, if...
strlen :: String -> Integer strlen x = length x example = ["dog", "cat", "orange"]
...then "map strlen example" would evaluate to the list [3,3,6].
The Haskell tuts never explain what '$' does that I have found, but if you look at any non-trivial Haskell code, it pops up all over the place. (I know what it does now through trial and error!).
Yup. That's a legitimate complaint. The '$' application function is under explained and mainly used to eliminate parenthesis.
And '.'. This is explained, usually as f . g x = f( g( x )) or f . g x = (f (g x ) ) depending upon flavour, but what that doesn't tell you is when you need to use it! In most dialecs, simple abutment of functions f g x achieves the same thing--except when it doesn't and the '.' is required!
Yeah, function application precedence was a little tricky for me at first in Haskell (and curried everything doesn't make it easier), but you'll get the hang of it with a little practice.
Enough ranting, (sorry you caught me after another fruitless search for information and examples, and several long and ultimately useless downloads of PDFs that I had to search manually because the stupid UI won't let me search for stuff that wasn't bloody indexed. Grrr!).
No problem! Ranting is par for the course when undertaking any new exercise. Hang in there, it'll get easier the more you do.

Replies are listed 'Best First'.
Re^13: World's shortest intro to function programming
by BrowserUk (Patriarch) on Jun 20, 2005 at 07:06 UTC
    Here's where I'll heartily recommend the book The Haskell School of Expression: Learning Functional Programming through Multimedia

    Well. As an interim measure I read the lecture slides. From that cursory glance--that actually went at about the right pace (4 or 5 slides representing a few 10s of pages)--it does at least appear to attempt to deal with (what I term) real-world problems.

    However, and here's the reason I will want to browse the book before purchasing rather buying on line, it (appears) to skips the nasty little details.

    For example, the treatment of the trivial, but strongly indicative example of the unix wc program. (Which I had to type in because it doesn't come as part of the source file distribution with the book)

    import System wcf :: ( Int, Int, Int ) -> String -> ( Int, Int, Int ) wcf ( cc, w, lc ) [] = ( cc, w, lc ) wcf ( cc, w, lc ) ( ' ' : xs ) = wcf( cc+1, w+1, lc ) + xs wcf ( cc, w, lc ) ( '\t' : xs ) = wcf( cc+1, w+1, lc ) + xs wcf ( cc, w, lc ) ( '\n' : xs ) = wcf( cc+1, w+1, lc+1 ) + xs wcf ( cc, w, lc ) ( x : xs ) = wcf( cc+1, w, lc ) + xs wc :: IO() wc = do name <- getLine contents <- readFile name let ( cc, w, lc ) = wcf( 0, 0, 0 ) contents putStrLn ( "The file:" ++ name ++ " has " ) putStrLn ( show cc ++ " chars " ) putStrLn ( show w ++ " words " ) putStrLn ( show lc ++ " lines." )

    Compiling and running that fails because it has no main. Okay, it's off a slide, so add  main = wc. Compile and I get a (500kb!) executable. Try running it--on a file with 2 lines of 30 spaces:

    C:\ghc\test>wc 60sp2l.txt wc: interrupted C:\ghc\test>wc 60sp2l.txt The file:60sp2l.txt has 62 chars 62 words 2 lines. C:\ghc\test>wc p:\test\1GB.dat The file:p:\test\1GB.dat has Heap exhausted; Current maximum heap size is 268435456 bytes (256 Mb); use `+RTS -M<size>' to increase it.

    Impressed? Not! Doesn't handle command line parameters. No prompt. Counts spaces and newlines as words. Can't handle a big file.

    Yes. I know it's only a demo. I should be able to add a prompt easily enough:

    wcf :: ( Int, Int, Int ) -> String -> ( Int, Int, Int ) wcf ( cc, w, lc ) [] = ( cc, w, lc ) wcf ( cc, w, lc ) ( ' ' : xs ) = wcf( cc+1, w+1, lc ) x +s wcf ( cc, w, lc ) ( '\t' : xs ) = wcf( cc+1, w+1, lc ) x +s wcf ( cc, w, lc ) ( '\n' : xs ) = wcf( cc+1, w+1, lc+1 ) x +s wcf ( cc, w, lc ) ( x : xs ) = wcf( cc+1, w, lc +) xs wc :: IO() wc = do putStr ( "Filename; " ) name <- getLine contents <- readFile name let ( cc, w, lc ) = wcf( 0, 0, 0 ) contents putStrLn ( "The file:" ++ name ++ " has " ) putStrLn ( show cc ++ " chars " ) putStrLn ( show w ++ " words " ) putStrLn ( show lc ++ " lines." ) main = wc

    Compile:

    C:\ghc\test>ghc -o wc.exe wc.hs wc.hs:9:8: Parse error in pattern

    Hmmm. Informative!

    So, skip the prompt, and try and get the argument from the command line. Scan the library docs. System looks promising. But there is nothing that looks like it gives me access to the commmand line? Okay, skip that. Try dealing with the "words are spaces" problem. In an imperative language I'd simple 'remember' the previous character and treat consecutive spaces (and newlines) as a single delimiter for the purpose of counting words...but of course, that's state!

    So, how about dealing with the memory issue? I thought the beauty of Haskell was that it was non-strict or lazy. That it dealt with infinite lists. Then why does getContents insist on loading the whole darn file?

    Another example drawn from the same source.

    From Chapter 8, containsS for Rectangles is defined as:

    Rectangle s1 s2 `containsS` (x,y) = let t1 = s1 / 2 t2 = s2 / 2 in -t1<=x && x<=t1 && -t2<=y && t2<=t2

    But there is a classical GUI problem here. If you divide the screen into a grid, of say 10 x 10 pixels, then by the above definition any point on the right edge of one rectangle also appears on the left edge of the adjacent rectangle to its right. Likewise for points on the other three edges and their corresponding neighbours. This leaves a point at the crossroads between 4 adjacent squares testing as being contained within all four squares simultaneously! Might work for a Corner bet in roulette, but it surely doesn't work well for the majority of hit-testing purposes in graphical applications.

    And that's the problem. All the demos are the same. They concentrate on (laborious formal) examination of Haskell's strengths and completely skip over all the messy edge cases.

    All languages have their strengths and weaknesses. What defines a languages usability is the way in which it deals with it's weaknesses. What puts the P in Perl, is the practical way in which it has built-in mechanisms for dealing with the messiness of the real world--the edge cases--even where that means it has to eshew orthoganality and purity in order to achieve that practicality. Where the same problem in usability crops up frequently, the language has been extended and "special cased" to deal with that situation in a reasonable and usually quite intuative way. And the special cases are the subject of extensive documentation in the FAQ.

    My problem with trying to get to grips with FP is that the FP language documentation reflects their theoretical origins by expounding on the formalism of their definitions extensively, but (from what I've seen so far) leaving the messy detail of dealing with the edge cases to .... I don't know yet. I haven't found the answer.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      So, how about dealing with the memory issue? I thought the beauty of Haskell was that it was non-strict or lazy. That it dealt with infinite lists. Then why does getContents insist on loading the whole darn file?

      I was curious about why this happened, so I tried my own variations to see if I could overcome it. Nothing I tried worked; Hugs ran out of memory with a large file every time. So I did some more searching online and through the Haskell mailing list. There's a thread that talks about the 'wc' program, and gives the reasons why it uses so much memory.

      In essence, it's not that Haskell is not being lazy enough and reading in the entire file. The problem is that it's being too lazy, and not evaluating the '+1's in the recursive function call. So it keeps all these '+1's around in memory, waiting to be evaluated until it hits the base case, but you run out of memory before it gets there.

      The solution they came up with on the mailing list was to create a data type to hold the stats, and put strictness markers on the data type so that the '+1's would get evaluated immediately. Below is your example converted to that style. The strictness markers are the exclamation points before the parametric types in the data constructor Stats. They tell Haskell to evaluate whatever value is about to be put into that slot in the Stats value being created, instead of keeping the "thunks" around to evaluate later. This version does run in constant memory space with a huge file.

      data Stats = Stats !Int !Int !Int wcf :: Stats -> String -> Stats wcf stats [] = stats wcf (Stats cc w lc) (' ' : xs) = wcf (Stats (cc+1) (w+1) (lc ) ) xs wcf (Stats cc w lc) ('\t' : xs) = wcf (Stats (cc+1) (w+1) (lc ) ) xs wcf (Stats cc w lc) ('\n' : xs) = wcf (Stats (cc+1) (w+1) (lc+1) ) xs wcf (Stats cc w lc) ( x : xs) = wcf (Stats (cc+1) (w ) (lc ) ) xs wc :: IO() wc = do putStr ( "Filename: " ) name <- getLine contents <- readFile name let (Stats cc w lc) = wcf (Stats 0 0 0) contents putStrLn ( "The file " ++ name ++ " has " ) putStrLn ( show cc ++ " chars " ) putStrLn ( show w ++ " words " ) putStrLn ( show lc ++ " lines." ) main = wc

        Thankyou! You have no idea how much that will help me with my chosen 'first program'.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      However, and here's the reason I will want to browse the book before purchasing rather buying on line
      Go to your nearest public library. If they don't have it, ask them to do an interlibrary loan. You'll get to browse it for a month before you decide if you want to buy a copy.
      For example, the treatment of the trivial, but strongly indicative example of the unix wc program. (Which I had to type in because it doesn't come as part of the source file distribution with the book)
      I don't remember any word counting problem in the book. So I grabbed my copy off the shelf and looked in the index. No luck. I skimmed it quickly. No luck. I can't even think what chapter it would be in. Maybe it doesn't come as part of the source for the book because it not in there? I don't know.
      Compile and I get a (500kb!) executable.
      Yeah, its a high level language. The current compilers statically link everything in the runtime into one executable. Just for comparison, my version of perl clocks in at over 1MB.
      wc.hs:9:8: Parse error in pattern Hmmm. Informative!
      It's that 2D syntax thing biting you again. You need to indent the "name <- getLine" line in to match up with everything else...
      wc = do putStr ( "Filename; " ) name <- getLine contents <- readFile name let ( cc, w, lc ) = wcf( 0, 0, 0 ) contents putStrLn ( "The file:" ++ name ++ " has " ) putStrLn ( show cc ++ " chars " ) putStrLn ( show w ++ " words " ) putStrLn ( show lc ++ " lines." )
      So, skip the prompt, and try and get the argument from the command line. Scan the library docs. System looks promising. But there is nothing that looks like it gives me access to the commmand line?
      getArgs
      And that's the problem. All the demos are the same. They concentrate on (laborious formal) examination of Haskell's strengths and completely skip over all the messy edge cases.
      I think the assumption is that the books provides a high-level overview of the concepts necessary and its up to the user to provide the nitty-gritty details which are important to them.
      All languages have their strengths and weaknesses.
      Yeah, Haskell isn't Perl. They live in different niches of the programming language eco-system. If you're looking for a better lanugage to bang out one-off scripts in 15 minutes, Haskell isn't what you're looking for. Before I discovered functional programming, I always thought there must be a better way of creating programs then I was currently doing. When I saw the light of FP, I realized that it went a long way towards making programming work the way I thought it should. I could now make good looking programs that weren't brittle. And they looked beautiful. Learning FP made me a better Perl programmer too. But I'm not going to tell you that FP is the one true way for everybody. If imperative curly brace Algol descended languages suit you fine, stick with them. Different strokes for different folks.
        I can't even think what chapter it would be in.

        The implementation is shown on the lecture slides for CH.3.

        I just assumed (probably wrongly) that it was drawn from the book. I will go find the book next time I'm in town (rare) and give it a good browse. I will say that I have learnt a lot from just the slides already and I thankyou for bringing it to my attention.

        It's that 2D syntax thing biting you again. You need to indent the "name <- getLine" line in to match up with everything else...

        Hmmm. Seems I used the tab key instead of the space key for that one line. Looked fine in my editor and I didn't notice the difference in the post till I just looked back. I'm still working on my editor configuration for Haskell. It now converts tabs to spaces on save.

        The error message could be a little more informative.

        GetArgs

        I looked under System.Console; System.IO; System.Info; System.Process; System.Environment didn't get my attention. Before I discovered functional programming, I always thought there must be a better way of creating programs then I was currently doing.

        Likewise, I really want to like Haskell. My point was not that Perl was perfect or that Haskell was in anyway limited--Autrijus is proving otherwise every day. I'd just really like to see a reasonably simple but complete program developed from start to finish, by a competent Haskell programmer, as a way of getting a step up into the use of the language.

        With most Algol like languages, it is fairly easy for me to pick up the source to a program who's function I understand and learn by reading and modifying it. I've tried this with Haskell, but it is so different, not just in the layout, but the entire way of structuring the program, that even the simplest of real world examples leaves me wondering where to start.

        Simple example: When should I use data, when type or newtype?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.