6 ms·
Functional Programming 101 with Haskell
- MaxGabriel 13y agoFor a 101, you may want to direct readers to install Haskell Platform as an easy way to get GHCI and all that set up.
- tdinkar 13y agoThanks for this, I added a link in the bottom.
- jamesbritt 13y ago... 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.
- hamburglar 13y agoYeah, 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. :/
- codygman 13y agoWhat 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!
- carterschonwald 13y agoAlso 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 :-)
- hamburglar 13y agoAs 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 `='
- tdinkar 13y agoStart 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
- navbaker 13y agoSince 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.
- coolsunglasses 13y agoYou'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
- gizmo686 13y agoI 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.
- deleted 13y ago[deleted]
- coolsunglasses 13y agoSorry, yes, misspoke (so to speak) because I had friends waiting for me to queue a Dota 2 game :)
- deleted 13y ago[deleted]
- freyrs3 13y agoThe 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 http://learnyouahaskell.com/starting-out#an-intro-to-lists
- idProQuo 13y agoHaving 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.
- freyrs3 13y agoA 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.youtube.com/user/jekor/videos
- laureny 13y ago> 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.
- idProQuo 13y ago
- res0nat0r 13y agoI'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/ http://learnyouahaskell.com/
- asattarmd 13y agoThe best intro for functional programming, IMO, is Joel Spolsky's "Can your language do this" (http://www.joelonsoftware.com/items/2006/08/01.html http://www.joelonsoftware.com/items/2006/08/01.html).
- copx 13y agoEvery 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.
- euph0ria 13y agoWhy is it not n! ?
- tdinkar 13y agoIt's actually 9* (1 + 8 * (1 + 7 * (1 + 6 ..... + 1)))
- winstonsmith 13y ago= [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) + ...))
- nbouscal 13y agoThere 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 + ...
- euph0ria 13y agoaargh, of course... thanks!
- winstonsmith 13y agoIt'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.
- coldcode 13y agoEvery time I see Haskell I am reminded of programming in Forth for some reason.
- codygman 13y agoPerhaps because of point free style?