5 Replies - 2989 Views - Last Post: 18 October 2010 - 05:52 AM

#1 guahguahmonster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 209
  • Joined: 29-August 07

That first mental leap in functional programming

Post icon  Posted 21 September 2010 - 01:20 AM

*
POPULAR

So I have a short personal story to tell. And maybe it's stupid, but I felt I had to get it out there somewhere where it might be heard by people who might understand.

I'm taking a course in functional programming, required of all CS majors. The second problem set for this course is out, and and as I was working on it, I found that I just couldn't manage to wrap my head around this one section on folding. Now, of course I knew a little bit about folding, such as summing a list via List.fold_left (+) 0 my_list, but what I didn't know was how much of the picture I was missing. We were asked to do some complicated computations with lists with OCaml, and we couldn't use the rec keyword (so no functions we wrote could be recursive). I sat and stared at it for a long time and couldn't puzzle this section out even after I had finished the rest of the problem set. So I dropped in to office hours, and at some point it was suggested to me, "you know, when you fold, your accumulator doesn't have to be a single value... it could be, say, a tuple..."

Something "clicked" at that point. I sloshed the idea around in my head for a bit, sketched out some ideas on the whiteboard, and slowly it was like a whole new world of possibilities was laid bare before me. I could probably even say my mental state at that point was like that of the guy who dreamed about gods writing Lisp. It sounds all really lame now that I say it out loud ("You can use a tuple or a list or something other than a single value as the accumulator in your fold, big whoop..."), but it was still enlightening for me because I realized you can accomplish some really cool things with that mechanism, so I guess even if nobody else can identify with me, I can still revel in my own little victory. The code just flowed after that. I knew what I needed to do, and how to do it, and it just felt like such an empowering experience.

I dunno. I'll probably get addicted to this higher-order function business. Next thing you know, I'll start recoiling in shock whenever I encounter a language where a function isn't a first-class object. I'll just feel like there's... something missing, I suppose, when I try to program in Java. In C++ I can fake it with function pointers, but it'll never be the same...

Enough rambling. I guess I'm posting this here to ask for any feedback (probably all I'll get is unanimous agreement that I'm just crazy or something XD), and to see if someone can whet my appetite for greater things to come (most likely though, I'll just have to learn these through trial and error like I did with folding).

Is This A Good Question/Topic? 6
  • +

Replies To: That first mental leap in functional programming

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: That first mental leap in functional programming

Posted 21 September 2010 - 02:18 PM

You are most certainly not crazy. Functional programming's concept of higher-order functions, and the composition of existing functions to achieve a result is unbelievably powerful and elegant. And, when you first discover the possibilities and things 'click', it's quite literally like 'seeing God'.

It only gets better. I remember when I was first learning about folding. It took a while for it to click but once it did, the experience was quite similar to yours. Little discoveries like that are what make coding worthwhile for me.

Wait until you learn about homoiconicity and macros and their significance in Lisps one day. You'll feel like you're on ecstasy.

Thanks for posting this. I enjoyed reading it.

This post has been edited by Raynes: 21 September 2010 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2873
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: That first mental leap in functional programming

Posted 21 September 2010 - 05:22 PM

I agrre with Raynes. I'm slowly learning Clojure myself (haven't yet gotten to folding), but some things do literally seem like magic. It's a refreshing experience and for a while, makes me wonder why I crawl back to places like Java.
Was This Post Helpful? 0
  • +
  • -

#4 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: That first mental leap in functional programming

Posted 21 September 2010 - 06:02 PM

Clojure doesn't actually have folding. It has reduce though, which is a left fold. It just doesn't have the variety of fold functions that Haskell has. A left fold is a serious utility knife.

Both languages do encourage the composition of higher-order functions rather than raw recursion.
Was This Post Helpful? 0
  • +
  • -

#5 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: That first mental leap in functional programming

Posted 29 September 2010 - 01:39 PM

Quote

You'll feel like you're on ecstasy.


Functional languages are potentially gateway drugs. But in a good way - If your fortunate they might eventually lead you into type theory, proof theory, formal systems in mathematical logic (lambda calculus, combinatory logic etc) and other great stuff!
Was This Post Helpful? 0
  • +
  • -

#6 hookiethe1  Icon User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,335
  • Joined: 28-September 10

Re: That first mental leap in functional programming

Posted 18 October 2010 - 05:52 AM

Congrats to you, I'm struggling really hard with SML programming in my languages class at the moment and hoping that something will click for me soon, otherwise I'm screwed to the point where I might get my first fail. :whatsthat:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1