Getting started with Functional Programming

For the last 6 months, I’ve been hearing increasing more and more about functional programming and how it can magically improve the way you write code. My first thoughts when hearing about functional programming were rather naive, they were along the lines of "Aren’t all programming languages functional?" (in the operational sense) obviously not really knowing what is actually meant by term functional.

I’m currently learning Haskell, which is a purely functional language. It is very fundamentally different from most object oriented languages in a structural sense, and even more abstract.

Here are a few links I am finding quite handy and should help you on your way to a zen mastery of the functional way of programming.

  • Introduction to Haskell - Interactive lecture slides for an introduction to Haskell from the University of Virginia.
  • Learn you a Haskell - A great and well paced book, which has also been published online for free for all!
  • List of Lambdas - A really well written blog post by Steve Losh. Takes you through a functional way of creating lists in JavaScript functions. This is what originally sparked my interest of functional programming. 
  • Functional Programming in 5 Minutes - Another set of slides posted recently on slid.es, which explains function currying through some examples written in JavaScript.
  • Functors, Applicatives, And Monads in Pictures - This is a slightly more complicated article regarding some more abstract concepts in functional programming, but it is well written.
  • Haskell Amuse-Bouche - A google tech talk from Mark Lentcnzer, this talk does a good job of explaining why people are really getting excited about Haskell & functional programming.

Anyways Good luck and hopefully you find these links of use to you.

anonymous asked:

Do you have any suggestions for people that are just starting out in the world of coding?

Well, I haven’t been doing this for very long - I’m still starting out myself. But I’ll do my best:

  • Language doesn’t matter. Just pick one and start learning. If you don’t like it after a while, pick a new one! Different languages have more similarities than you’d think, so you won’t be wasting your time. However:
  • If you’re interested in programming for the sake of writing elegant programs, then I’d recommend starting with Python, or Haskell. These both have strong communities, so you should be able to get help/tutorials just by googling. If you want to make graphics, I recommend Processing, and for audio you could try SuperCollider. But don’t limit yourself. If you see an interesting tutorial for a language you don’t know, skim read it anyway. Chances are the concepts will be pretty familiar.
  • Everything you need to know is on the internet. Try googling ‘learn programming python’. Open all the links on the first page, close the ones that look boring, then read the rest. Some pages even have interactive tutorials, which are fantastic. (codecademy.com and learnpython.org are great places to start.)
  • Try to write code every day, especially when you’re starting out. It’s often tricky to know what to write, so until you’re inspired, read through online tutorials and work through the exercises. This is pretty daunting, and at first nothing will make sense - which is fine. Often you’ll be reading for hours and you’ll feel like nothing’s sticking, but then it’ll just click. But the only way it can click is if you persevere with the initial reading. The interactive pages help a lot with this, and they’re more interesting than just reading lots of dry technical stuff.
  • There’s a difference between giving up and taking a break. If you’re getting frustrated, go and do something else. Your brain is processing what you’ve learnt, even when you’re not reading, coding, or even consciously thinking.
  • If you have any friends who are also interested in programming, talk to them (or learn alongside them!). Talking about concepts will help you to solidify ideas.
  • Actually writing code consists of two things: thinking and typing. Don’t undervalue either. Learn how your brain works, how you learn best, when you’re most awake and productive. Also consider learning to touch-type. And find a nice text editor! I recommend Sublime Text 2 - it’s got lots of things like syntax highlighting, line numbers, tabbed editing, and code completion that make coding much faster. Also it looks super rad.

Translating into swedish with Haskell.


So I have a new function that’s leaking space:

data C3 a = C3 a a a

c3_0 (C3 x _ _) = x
c3_1 (C3 _ x _) = x
c3_2 (C3 _ _ x) = x

splitBands :: IOArray Int (C3 Double) -> IO (C3 (IOArray Int Double))
splitBands vc = do
b0 <- mapArray c3_0 vc
b1 <- mapArray c3_1 vc
b2 <- mapArray c3_2 vc
return $ C3 b0 b1 b2

I know there are probably more concise ways of writing this, but this is the simplest version of the function that exhibits the problem. It takes a mutable array of containers of three doubles, and converts it into a single container of three mutable arrays of doubles. And it leaks. Apparently.

I’m super lost. Like, pretty much the only bits that do anything are the mapArrays, and they’re library functions so they probably shouldn’t leak, right?