TIL everything in Lisp is a function

I’m current reading Structure and Interpretation of Computer Programs as part of a study group we started during Pablo’s Fiesta. Our goal is learn Haskell by reading this book and implementing all of the exercises in Haskell instead of Lisp. So far its been really fun and it’s actually forcing me to learn both Haskell and Lisp at the same time.

I find Lisp to be a fascinating language and it actually makes a lot more sense to me than Haskell does. Going through the exercises we came across this really interesting piece of code.

``````(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))``````

Ok so what’s going on here?

• if b > 0
• return +
• else
• return -

I think it is really cool that symbols like + and – are just functions, therefore you can return them from an if statement. Here is how this might look in javascript.

``````function a_plus_abs_b(a, b) {

return (b > 0 ? plus : subtract)(a, b);

function plus(a, b) { return a + b; };
function subtract(a, b) { return a - b; };
}
``````

Arguably not as cool as the Lisp example but it helped me understand it better.

OMG whats the point

The point is go out and learn you a Haskell (or any functional language other than javascript ). It’s for a greater good. I had never thought of + and – as just functions and now I’m kind of pissed that other languages don’t let you exposed them as such. Or give me a choice like Haskell does.

``````add :: Integer -> Integer -> Integer
add (a, b) = a + b;

-- Haskell allows me to call the function two ways

-- prefix notation

-- infix notation

Herp Derp,

-Ryan

My name is Ryan Rauh and I like shiny things. If I've seen it on twitter more the twice most likely I've looked at it or tried it. I really like memes and other silly and seemingly useless things out on the internet. I blog about things *I* think are cool and interesting, I hope you will find them cool and interesting too.
This entry was posted in Functional Programming and tagged , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
• nice! love the baby pic

I started learn you a haskell last week. I’ve only gone through the first section, but it’s already blown my mind a number of times, and is helping me make sense of otherwise crazy and confusing things in some of my usual languages (like javascript, ruby, c#)

• Anonymous

Sweet, well I look forward to reading all the insights that you come to as well. It’s taking me a long time to go through SICP mostly because I get tripped up on concepts that I already know. It’s like year one kinda stuff too, like normal-order vs. applicative-order concepts I already knew just didn’t know the term for them

• phil jay

I have a question to someone experienced with lisp or generally functional programming: Could you read this from 10 feet and know what it does at the first gaze?? I mean it’s like wtf a first and then like wow awesome! But can I even get used to it at all?

• Ryan Rauh

First off I am merely beginning to learn lisp and functional programming.

I’ve heard the saying “write once, read never” in regards to lisp. From 10 feet it is really confusing still to me.

I didn’t really start understand lisp until I read up on abstract syntax trees and once you understand that concept lisp gets much easier to read and understand.

When is saw (if  (> b 0) + -) is was like omg that’s kind of amazing!