Hacker News new | comments | show | ask | jobs | submit login
Functional Programming 101 with Haskell (blog.gja.in)
94 points by Who828 on Jan 24, 2014 | hide | past | web | favorite | 40 comments



The equivalent chapter in Learn you a Haskell is probably a better place to learn about list processing in Haskell.

http://learnyouahaskell.com/starting-out#an-intro-to-lists


Having finally finished my first read of LYAH (It took me months of reading, practicing and taking breaks to let it all sink in), I can only see one way to improve upon it: Make a video series.

I don't know enough about Haskell to pull it off (though I may try if no one's done it in a year or so). However if someone could inject the wacky sensibility* of things like LYAH, Computerphile and CodeSchool.com into a Haskell video tutorial, it would do worlds of good for the community. Better yet, if someone could make an interactive class like CodeSchool or Codecademy, that would be amazing.

* Thought I should mention that the wackiness isn't the only important part, it also needs to be properly paced. That was LYAH's other strong point, they hammer home each concept with multiple examples before moving on.


> Having finally finished my first read of LYAH (It took me months of reading, practicing and taking breaks to let it all sink in), I can only see one way to improve upon it: Make a video series.

Why? I can't think of any advantage that a video has over an article when it comes to teaching computer science. An article allows me to read and understand each concept at my own pace and rereading something I didn't quite get is trivial. All these simple things are just tedious with a video.


While I don't need videos in order to learn, I find that seeing concepts represented visually while hearing them described by someone often works better than simply reading them. I've done some of CodeSchool's courses even though I already kind of knew the material, just because seeing it presented in that format helped reinforce things.

Furthermore, having coding exercises with a checker that can tell if you've done things correctly would help a ton when it comes to actually writing Haskell code.


I loathe videos for stuff like this, and avoid them like the plague, but I suppose that for some people they work due to the way those people learn.


Plus, the article is computer-searchable (Ctrl-f) and you can more easily point somebody to a specific passage or quote it.



For anyone trying to go to the Erik Meijer link, the correct url is: http://channel9.msdn.com/Series/C9-Lectures-Erik-Meijer-Func...

I think vasquez put an extra right angle bracket by mistake.


Thanks. I did fix it soon after posting, but guess the incorrect one could be cached for a while.


Erik recently announced that he will re-do this course in one of the online course websites. (no twitter from work so don't have details)


A video series or course on LYAH would be an amazing resource. On an aside, there's a bunch of great intermediate level video tutorials working through various projects here:

[1] https://www.youtube.com/user/jekor/videos


https://www.fpcomplete.com/ is trying to do this


I'll chime in and mention that Learn You A Haskell is the best online Haskell tutorial I've found. I've been working through bits of it the last couple of weeks and it is excellent.

Also the artwork is great.

http://learnyouahaskell.com/


As a complete and total haskell noob, I am unable to even get through your syntax examples:

Prelude> sumOfElements [] = 0

<interactive>:12:18: parse error on input `='


This defiantly falls under Haskell not being noob friendly.

If you are in the REPL, you want to do `let sumOfElements [] = 0`

If you want multiline input, you open with `:{` and close with `:}`. Both of these need to be on their own line. For example:

    Prelude>:{
    Prelude| let sum [] = 0
    Prelude|     sum (x:xs) = x + sum xs
    Prelude|:}
As tdinkar points out, it is often easier to define your functions if a file and load that file from the REPL.


aha, i tried using 'let' on the assumption that he was just being brief, but I put a 'let' in front of each of the pattern-matching function signatures, which didn't work. Thanks.

edit: I take your last comment as "when using the REPL, it is easier not to use the REPL." :D


Since Haskell has a strong divide between compile and runtime, the REPL is this weird pseudo-runtime. It's best not to think of it like a REPL except for very simple use cases and instead think of it like a command prompt that you can evaluate some portion of Haskell in.

If you type `:help` you'll see all of the colon-commands that make up the command language. I'm not a Haskell expert, but my naive workflow tends to be a lot of usage of `:reload`. As a Lisper, I kinda feel like the colon commands should have been the default (sans colon) and one of the commands should have been `eval <haskel code>`.


Check out IHaskell - https://github.com/gibiansky/IHaskell

It lets you do multiline stuff, and you get to see the output right away. You don't need let, or to repeat the "main = do ..." every time you want to run a block of code.

I think it's really helpful when you're starting out.


Neat, I knew about `:set +m` for enabling multiline input, but not `:{ ... }:`.


You're using the REPL, where every operation is basically happening in a do monad.

If you get an error like that where you intend to define something, use let.

like: let sumOfElements [] = 0


I believe you mean that every operation is happening in the IO monad, and you input is being interpreted as if it was in a do block.


Sorry, yes, misspoke (so to speak) because I had friends waiting for me to queue a Dota 2 game :)


Start with downloading the hydra.hs file I linked to in the end.

Haskell is not like ruby, it's a bit harder to define functions in the interpreter. It's easier to put them in a .hs file and then :load file.hs


Since this is a 101, you might want to put your Haskell setup URL and any file dependencies/explanations of Haskell's idiosyncrasies at the top of the tutorial. A 101 is aimed at people who don't know anything about the language and assuming knowledge is a good way to ensure folks get frustrated and never come back.


The best intro for functional programming, IMO, is Joel Spolsky's "Can your language do this" (http://www.joelonsoftware.com/items/2006/08/01.html).


Every newer language can do "this". First class functions and anonymous functions have been a standard feature for a while. Does not mean that say Lua or Ruby are functional programming languages. If you just require the ability to pass around anonymous functions even C++ is a functional programming language these days.

To meaningfully distinguish functional from imperative programming you have to add purity/immutable data to the definition at least.


For a 101, you may want to direct readers to install Haskell Platform as an easy way to get GHCI and all that set up.


Thanks for this, I added a link in the bottom.


... I added a link in the bottom.

Perhaps helping users get set-up should be the first thing you address.

Also, you start off by saying the examples should work well in ghci but then show examples that are not run in ghci.

I fear your experience is blinding you to the numerous little things that trip up newcomers.

You might also state upfront what you assume the reader already knows. Such as the existence, and use, of ghci.

If you are mentioning ghci at the start then maybe show the examples as run in ghci. Or, if you prefer files, then explain how to run a file from the command line.


Yeah, the statement at the top that "you can run most of these examples in ghci" really tripped me up. On one hand, it gave me the clue that the haskell REPL is called "ghci", so I just went to my ubuntu box and typed "ghci" and got told I needed to install "ghc", which I did and was on my way. On the other hand, the examples weren't actually able to be run in ghci. :/


What you can do is:

1. paste all (well most, I haven't verified it to say all) of the functions in "anyfile.hs" 2. cd to the same directory as anyfile.hs in terminal 3. open ghci 4. :load anyfile.hs 5. Play with the functions!


Also you should make sure that those who hit the cpp problem don't get overwhelmed. I've actually spent some time this week working out a patch that will make the cpp problem tst hit OS X IMPOSSIBLE to have in the future. Did require learning some autoconf thogh :-)


Every time I see Haskell I am reminded of programming in Forth for some reason.


Perhaps because of point free style?


Why is it not n! ?


It's actually [n!e] - 1 where [x] the greatest integer less than or equal to x and e is the base of the natural logarithm. As nbouscal points out, the total number of heads is H=n!(1+1/1!+ 1/(n-1)!). Using the Taylor series for e^x, H = n!e - n!(1/n! + 1/(n+1)! + ...) = n!e - 1 - r, where r = (1/(n+1) + 1/(n+1)(n+2) ...). The remainder term r is easily seen to be less than 1, and H is and integer, so H=[H+r]=[n!e]-1.


There will be n! of the smallest heads, the ones which don't spawn any new heads. Total heads, though, will be 9 + 9 * 8 + 9 * 8 * 7 + ...


aargh, of course... thanks!


It's actually 9* (1 + 8 * (1 + 7 * (1 + 6 ..... + 1)))


= [9!e] - 1

(because your expression expands to 9 + 9(8) + 9(8)(7) + ... 9! = 9!(1+1/1 + 1/2! + ... 1/8!) = -1 + 9! e - (1/10 + 1/(10 11) + ...))




Applications are open for YC Winter 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: