

Teenage Haskell - lelf
http://twdkz.wordpress.com/2014/06/26/teenage-haskell/

======
MarcScott
My brother (a Haskell programmer) and I (a teacher of high school CS) have
talked about this often. He's postulated that teaching Haskell to kids with no
programming experience would be an interesting experiment. Their lack of
preconceptions regarding programming might make Haskell much easier for them
to pick up, than it would for a student with experience in another language.

What would be more interesting however, would be to teach a student Haskell
first, and then introduce a language such as Python. Would they think Python
was stupid or would they be grateful that all of a sudden they could use a for
loop?

~~~
seanmcdirmid
> Their lack of preconceptions regarding programming might make Haskell much
> easier for them to pick up, than it would for a student with experience in
> another language.

Or traumatize them forever away from the field of programming.

> Would they think Python was stupid or would they be grateful that all of a
> sudden they could use a for loop?

I would love to know the answer to this, regardless of my own biases. But
Haskell really does require discipline to use, and the kids would probably
appreciate the more lax nature of Python.

~~~
MarcScott
It's an experiment that I'll never run. CS exam requirements are geared
towards OO languages. Students have to analyse and write pseudocode using
traditional programming constructs.

~~~
seanmcdirmid
What are CS exam requirements? Are they standardized? I went through the
American system, and never had any of my tests biased to OOP (except of
course, a Smalltalk chapter in one PL zoo class).

Object thinking is easier to wrap one's head around than mathematical formal
thinking, but that's just because the former uses the brain's innate
linguistic hardware (we learn to talk without much effort) while the latter
requires training and practice.

~~~
MarcScott
I teach in the UK. Here's the spec for one of our GCSE courses.
[http://www.aqa.org.uk/subjects/ict-and-computer-
science/gcse...](http://www.aqa.org.uk/subjects/ict-and-computer-
science/gcse/computer-science-4512)

------
Velox
At the University of Edinburgh, the first programming class we do in first
year is an introduction to functional programming, which uses Haskell as the
main language (it helps that Phil Wadler is the lecturer). Lots of students
take to it much more easily than Java which is taught in the following
semester. It definitely seems that viewing Haskell as a "hard" language is a
product of an imperative programming education.

------
p1esk
I never tried Haskell, or 'functional programming". To be honest, I don't even
know what functional programming is. Can someone explain how is it different
from Python or C (the only two languages I did little coding with)?

~~~
cdsmith
Basically, the difference is that in a functional programming language, words
like "variable" and "function" have the same meaning they do in mathematics.

In C or Python, a "variable" is a storage location in which you can place
different values at different times. This is completely different from
mathematics, where "x" doesn't equal something different just because you
progress to the next step of computation. (But "x" might refer to something
different in a different _context_ , such as when evaluating a function with a
different argument; that's the sense in which it varies.)

Similarly, in mathematics, there's no such thing as a function that returns
something different each time you evaluate it, or that gives one result on
Wednesday but a different result on Friday, or that even gives no result at
all but causes letters to appear on a nearby computer screen. Yet in C or
Python, all of these things are called functions.

At first, it might seem surprising that you can do much programming without
the C or Python version of functions and variables. But:

1\. You can do more than you think without reassigning variables or using
functions that aren't "real" functions in the sense of mathematics, and

2\. Of course, in the end, all languages - including functional languages - do
have a way to create boxes to store values over time, or to perform actions
that make things appear on computer screens. It's just that in a functional
language, these things are not the basic bread and butter tools of programming
to the extent that they are in C or Python. So you have variables and
functions (in the mathematical sense), and you _also_ have other things that
act like storage locations and actions, but you only use the latter when you
have a real need for them.

That's the general character of the difference. There is, of course, a lot of
detail that I am leaving out in a response on a web site.

The reason people were speculating about whether this would work better with
people not yet accustomed to programming is that it's often thought that
experience in an imperative language (like C or Python) leads you to naturally
try to do things with actions and storage locations, even when it's not
necessary... and then it can get frustrating and confusing to work in a
language where most things are actually _not_ done that way. There's a theory
that says if you maybe learned the functional way first, you wouldn't find it
as frustrating.

I'm of two minds about that. I spent a year teaching Haskell with this system
(I'm the Chris Smith mentioned in the article), and I found that it's partly
true that students don't get as frustrated... sometimes. But there are other
cases where students naturally fall into trying to do things in imperative
ways, despite not having past experience in a programming language. I actually
think, computer programming aside, that this is one of the things that trip up
kids in math classes. So it's not just programming languages where this comes
up: a lot of teachers present math as step-by-step processes, as well.
Essentially, they introduce imperative programming without the computer. This
is what happens when teachers tell students that parentheses are about what to
do "first", instead of about which sub-expressions are meaningful on their
own. Partly, I think this is just how the human mind works, too, and it's an
inherent tendency that we have to overcome to really "get" algebra and other
mathematics in a deeper sense than knowing how to do specific problems. It's
still frustrating, then, for kids to adapt to having to describe processes
using equations and expressions, which at their core do _not_ list things to
do, but instead describe and state facts about relationships between
quantities.

I still think it's an important skill to learn. Indeed, I'm a lot more
interested in teaching that kind of reasoning than I am about teaching
computer programming. That's why I made changes in the new CodeWorld (the site
discussed in this article) that are designed to deliberately thwart students
who try to think of their programs as sequences of actions, and encourage
those who think in terms of compositional expressions.

~~~
p1esk
Thanks for your response. I've read it several times, but unfortunately it
didn't click for me.

>>>Similarly, in mathematics, there's no such thing as a function that returns
something different each time you evaluate it, or that gives one result on
Wednesday but a different result on Friday

I don't get this example. If the function is f = t, where t is time, then f
will give different results on Wed and Fri.

In both math and Python, a function such as Square = x*x will return exactly
the same thing as long as x does not change.

>>>This is completely different from mathematics, where "x" doesn't equal
something different just because you progress to the next step of computation

When I do math (such as algebra, or calculus), I think of a variable x as a
container that can hold any value. This is exactly how I think of a variable
in C or Python.

For example, I can build a graph of a parabola in Python. The way my code does
it is pretty much the same as if I did it by hand with a pencil: I assign
multiple values to x, and find corresponding values for y.

I can't quite pinpoint the source of my confusion...

~~~
cdsmith
> I don't get this example. If the function is f = t, where t is time, then f
> will give different results on Wed and Fri.

I assume you meant something like f(t) = t?

If so, then right, if you pass in the current time as a parameter to the
function, then the result can depend on it. But in Python, you don't pass in
the current time. That's because "function" means something entirely different
in Python.

In math, at the simplest possible level [1], a function is a set of ordered
pairs, with the property that no two different ordered pairs have the same
first element. That's all there is to it. So when I write f(x) = 3x - 5,
that's just shorthand for saying that f is the infinite set { (0, -5), (1,
-2), (2, 1), (1/2, -7/2), ...}. That's all there is to it. There's no
sequences of operations, no process, etc. It's just a set of ordered pairs.

Python's functions, on the other hand, are at their core a sequence of
instructions. Some of those instructions (like loops or conditionals) have
other sequences of instructions as components. They share an environment,
which consists of storage locations called local variables. A Python function
is actually a pretty complex thing, when you get down to it.

You could _model_ Python's functions using standard mathematics, if you play
some tricks: add hidden parameters, give interpretations to the results in
terms of physical effects, etc. But this just tells you that math is powerful
enough to model Python functions (and, well, anything else) in terms of its
much simpler ideas. It doesn't mean that those ideas are the same as the
things in Python that go by the same name.

> For example, I can build a graph of a parabola in Python. The way my code
> does it is pretty much the same as if I did it by hand with a pencil: I
> assign multiple values to x, and find corresponding values for y.

Okay, but you're describing the process of drawing the parabola. In math, f(x)
= 3x^2 + 2x + 1 isn't a _process_ for drawing a parabola. It _is_ a parabola.
So f, here, _is_ itself the set of ordered pairs, which make up a parabola if
you treat them as points on the coordinate place. That's the basic difference.

[1] Okay, for the pedants out there, this depends on your choice of
foundations. I'm assuming the standard interpretation using ZFC, but a similar
point would apply if you prefer a different foundational theory.

~~~
p1esk
Ok, I think I can now clearly see the difference between a variable or a
function in math, and in Python.

However, math and computing are not the same. How can you possibly represent a
concept of an infinite set in a computer language? After all, any computer is
a Turing machine, and the sequence of instructions is the very foundation of
computing (at least that's how I think about it).

It's interesting: I see many replies to my question, however no one has been
able to show me a clear example of some code in FP language, side by side with
the same code in Python or C, and point out how the FP code is better (in any
way).

I learned programming concepts by studying computer architecture, and after I
wrote bunch of MIPS assembly, I could clearly see how C makes my life easier.
Then I looked at Python, and I could see, for example, how a concept of a
class/object makes my life easier compared to C. So now I'd like to see how FP
can make my life easier compared to C or Python. I still don't get it.

~~~
cdsmith
There are various arguments for the benefits of functional programming. But
that's really beside the point here.

In this particular case, the point was that I created an educational tool for
teaching mathematics. It happens to involve a programming language; but the
goal isn't to train computer programmers. It's to teach math. So the language
used is one that lets you describe functions and quantities the same way as
you would in math, and mostly avoids the need to think about a computer doing
some sequence of operations.

------
arianvanp
How about [http://elm-lang.org/](http://elm-lang.org/) ? It is very similar to
haskell but a bit less daunting and all its libraries are focussed on building
inbrowser games. Sounds like something kids would love.

Now that i am brainstorming anyway. Something like
[http://scratch.mit.edu](http://scratch.mit.edu) would be so much nicer if it
were a functional language! Instead of confusing blocks that maintain state
every block is just a standalone welldefined function and combining blocks is
just functional composition and application! I think it would be reaallt easy
for a kid to digest mentally: blocks have inputs and outputs, thats it.

Does anybody know if there are projects tjat accomplish this?

~~~
seanmcdirmid
Elm is this weird language that is really good at visual demos but is not
anything interesting as far as language goes (if you want Haskell, just use
Haskell, if you want FRP, there are plenty of libs for that, if you need
pseudo FRP on web, then elm makes sense).

Scratch would be impossible to use as a functional language, especially for
the logo-style turtle procedural behavior they are aiming at.

------
codygman
This is awesome! I might have to contribute to codeworld, it seems like a
really worthwhile project.

------
tzs
How did Sam get his name into his sheep picture? I don't see any functions
that draw text among the seven functions the kids had at that point, and the
text doesn't look like it was built by just drawing rectangles and circles.

~~~
chowells
If you go to the site, you'll find a link to the docs in it. They're not
complete, but they do at least list the functions available. One of them
renders text as a Picture.

------
superuser2
At UChicago, there are two introductory sequences. The Honors course, intended
for students who are already comfortable programming, is taught entirely in
Haskell.

Here's what it looks like:
[http://brick.cs.uchicago.edu/Courses/CMSC-16100/2013/lecture...](http://brick.cs.uchicago.edu/Courses/CMSC-16100/2013/lectures.php)

The normal introductory CS course is taught in Scheme. Actually teaching -
even acknowledging the existence of - functional programming was a major
reason I chose that department.

------
estebanrules
Another great resource to learn functional programming. Thanks.

------
emsy
Cause I'm just a teenage haskell baby, use monads for a type named Maybe read
papers on lambda calculus with me Ooohoo Hoo Hooooooo

------
Kiro
(&) :: Picture -> Picture -> Picture

Can someone explain this line?

~~~
m0g
It's a type declaration saying that the & operator takes two Picture as
arguments and returns a picture.

So, if "a" and "b" are pictures "a & b" is typechecking.

To be more precise, if you account for currying, it says that the & operator
takes a Picture and returns a Picture -> Picture function. This function can
then be applied to a Picture to yield a Picture. It's as I explained above but
with an intermediate step.

