Official SICP/Scheme Study Group Registration (up. 1/26)

Started by
65 comments, last by kSquared 17 years ago
Quote:Original post by Anonymous Poster
I think it might be a good idea to write a short conceptual introduction to what functional programming entails and how it is different from imperative style. Failure to grasp this (there are OTHER ways of writing programs than as sequences of assignments) leads to pain, as Alpha_ProgDes and others have illustrated in the past when they tried to tackle SICP.

Presumably, many students will have prior programming experience with imperative languages only, which is annoying for them because they'll try to write C-style scheme and do worse than the absolute beginners.

So I'll try to whip up a short primer on the conceptual underpinnings of FP and imperative style tomorrow, with plenty of examples, and submit it for comment and review. Forewarned is forearmed, and much frustration may be avoided I think if we can get students to understand this early on.
Good idea. I'll take a look at that and see if I can contribute.

Before we start we need to wait for word from Muhammad Haggag on whether a workshop forum can go ahead now that we have a decent line up. Hopefully we can still aim to interest total beginners to join the group too.

I am interested in joining the group as a: Mentor
Experience: C++, functional languages
Timezone: GMT
Advertisement
Quote:Original post by Rebooted
Before we start we need to wait for word from Muhammad Haggag on whether a workshop forum can go ahead now that we have a decent line up.


That'd be great. If nothing happens soon, I'd hate for us to lose momentum, so I'd like to start it as a thread here. Or, maybe we should wait so that all the threads can be in the right forum. I guess there's no real hurry, but I want to make sure actually do this :D We'll wait until we at least here from Muhammad.

Quote:
Hopefully we can still aim to interest total beginners to join the group too.


Any ideas on how to make sure we don't aim for anything more than beginners, besides finding the right pace?
Name: Joanus.
I am interested in joining the group as a: Student.
My experience with Scheme is: Did a bit with Prolog (same family of languages, I think) along with some Haskell (functional, but not even remotely similar to Scheme)
My experience with general programming is: Experienced (at least, I like to think so [smile]). Professional software engineer, mostly C++ and C# but have used quite a few different languages from across the spectrum.
Timezone: GMT+10
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
I am interested in joining the group as a: Mentor
Experience: imperative and functional
Timezone: GMT+1
Quote:
It's nice that someone would take time out of their day to write a tutorial on functional programming but the little schemer is about 200 hundred pages and they had 2 follow up books because they couldn't properly describe the gist of functional programming in that space.


Are you the next James Joyce?

Does "a short conceptual introduction to what functional programming entails and how it is different from imperative style" mean "a tutorial on functional programming" in your world?

Does "the gist of functional programming" mean "learning the Scheme programming language and loads of scheme techniques in addition to functional programming techniques and everything else in TLS" in your world?

The gist of functional programming is the evaluation rule in the lambda calculus: substitution as computation (and hence referential transparency) instead of mutation of the contents of a variable.

Functional programming techniques are another matter, but the gist of functional programming is so simple that schoolchildren use it in math class all the time. Only when their minds are corrupted by basic does functional programming look weird.

So: I'll ask the students to skim a few paragraphs we'll collaboratively write that'll hopefully save them trouble when tackling the real thing (SICP).

You ask them to buy and read a few books before starting on another book. In a week.

Let's see what approach wins out. ;)
Here's a rough draft. Have at it, especially if you can think of ways to make it shorter and clearer. I chose python as an example language because its syntax is familiar to imperative programmers and because it's dynamic like scheme:

If you have programming experience, it's possible you've only used imperative style until now.

What is imperative style?

You're probably used to thinking about your computer as a processor attached to some memory. Variables are like buckets that can hold values, and our program can change the values these buckets hold.

An imperative style program is just (roughly) a sequence of changes to the contents of the program variables. Computation proceeds by explicitly changing the values in the variable buckets. If you've only ever programmed in imperative style, this is as natural as breathing.

For example, let's make a program that adds 4 to a number and then multiplies the result by 2. An imperative way of expressing this (in python) would be:

def example1(n):    x = n + 4    x = x * 2    return x


We create a variable, x, and then modify its contents in a sequence of steps until it holds the result we're looking for.

What is functional style?

Functional style means viewing computation as evaluation of functions. There are NO variables in functional code in the sense we described above: that is, there are no variables that represent memory locations whose contents can change. There are NAMES bound to VALUES (you can bind x to 4, sure) but these don't stand for mutable memory locations. They're just synonyms for the value they're bound to.

So there's no hidden state you can modify in purely functional code; there's nothing like

x = x + 4

because x doesn't stand for a memory location whose contents can change. Expressions (the right hand side of the statement x = x + 4) can be evaluated, but their result can only be returned from a function or bound to a name: it cannot be assigned to any variable.

State can only be passed around the program explicitly as parameters in a function call, or returned as the result of function evaluation. This is the take-home message.

The above example would look like this:

def example2(n):    return (n + 4) * 2


A name, n, is bound to a value (whatever the function example2 is called with). Functions are called (in this case, the built-in functions + and *). A value is returned: the result of evaluating (n + 4) * 2.

But NO assignment is taking place: no variables are being modified.

A less trivial, canonical example: the factorial function

n! = n * (n - 1) * (n - 2) * ... * 1

We might write this imperatively as a loop, which counts from 2 to n and uses a variable i for iteration and a variable accum to multiply all the i's together:

def fact1(n):    accum = 1    for i in range(2, n+ 1):        accum = accum * i    return accum


Assignment is taking place both for i (it changes every iteration) and accum.

How could we write this in a functional style? You're probably familiar with the concept of recursion:

def fact2(n):    if n < 2:        return 1    else:        return n * fact2(n - 1)


Notice no assignment is taking place: within fact2, n is bound to whatever the fact2 is called with, and functions are called and values returned, but no variables are changed.

Functional programs are like math equations. The "x" in a quadratic equation doesn't stand for a memory location that holds a particular number at a particular time, which changes during program execution. Neither does the x in a functional program.

Bear this in mind when you're starting out, and take a deep breath whenever you feel the urge to assign values to variables. Scheme does permit assignment (it's a multiparadigm language, not purely functional), but we'll do without it first.

If you want to pass state around, you'll have to return it as the result of a function: no assigning to a variable is permitted. You'll get the picture when you start working on the examples, but at least you're forewarned: do NOT try to write scheme as you would write C++.
Name:Nitage
I am interested in joining the group as a: Student
My experience with Scheme is:1st year university course (5 years ago) taught using scheme. No experience of scheme, lisp or any other functional language since then.
My experience with general programming is:Expert C++, Python. Professional Software Developer.
Timezone:GMT
I had to explain The Functional Way a few times too, and I did it the same way as you. Whether this means your explanation is good or not I don't know, perhaps I should ask the people I tried to enlighten. Maybe you should register so that it's possible for you to edit your posts should it be necessary.

What might be missing are advantages and disadvantages: that purely functional programs don't know the dimension of time and hence are easier to reason about, persistent vs ephemeral data structures, possible problems with modularity/encapsulation, but that's perhaps a little too much for an introduction. Anyway, I like to point out these (dis)advantages as they come, so that the imperative programmer doesn't feel like his hands are tied up by seemingly arbitrary limitations.

I'd also like to mention Why Functional Programming Matters, but their view of what "functional programming" is is a bit different from what was explained here (stateless vs higher-order).
Well, some may be aware of this, others may not, so, figured I would chime in with a link to MIT:

SICP

From there you can pretty much download the course materials and such. Just a heads up to the originator for an extra source for information.
SamLowry:

My experience is that students with an imperative background have the most trouble with the concept of statelessness, because it's seldom mentioned explictly (or at least explicitly enough, it seems). They stumble on the initial examples in SICP and elsewhere because they try to write them imperatively.

As to how clear the explanation is... probably not very, I admit. Help me fix it; the text I posted is meant as discussion fodder.

I actually intend this as more of a cautionary note for imperative programmers than a real introduction: that's what SICP is for. So I think it should be as short as possible so people actually bother to read it (shorter than it is now).

Maybe another cautionary explanation should be given about binding vs. assignment.

Ah, and I love the Why FP paper, it's one of the things that keeps me warm during the functional winter. It should go in the links section. You're right that it's skewed more toward more pure languages than scheme; it'll still be very relevant when we get to SICP's lazy evaluator. And it's very motivational.

This topic is closed to new replies.

Advertisement