Tools to learn functional programming?

Status
Not open for further replies.

MrMills

Ars Praefectus
3,727
Moderator
Since it has been mentioned in here a few times, I've become curious about learning functional programming (specifically Lisp or O'Caml). However, my half-hearted attempts so far have not helped me much.

I'll do some Googling, but remember I have the beginner's paradox: I have no means to judge what is good and what isn't. Anybody have suggestions on what tools to use (preferably Win32-based, jemfinch :)) while learning about such a thing? For instance, if I was learning Perl, I'd want to know about ActivePerl. And if I was learning Python, I'd want to know about PythonWin.

Also, any good tutorials or introductory material would be useful.

Thanks in advance.
 

Prototyped

Ars Legatus Legionis
11,126
Try looking at online lecture notes for university classes that cover functional programming.

Examples:

http://www.md.chalmers.se/~rjmh/tutorials.html
http://www.cl.cam.ac.uk/Teaching/FuncProg/FuncProg.html
http://www.cl.cam.ac.uk/Teaching/Lectures/funprog-jrh-1996/index.html

There's a book available online called Common Lisp the Language, 2nd Edition, by the renowned Guy L. Steele, Jr. -- but some pages are, regrettably, missing.

Edit: A more complete mirror is available.

Standard ML interpreter/compiler: Available at the SML/NJ site (Win32 binary)

Scheme interpreter: MzScheme

Common LISP implementation: GNU Common LISP

O'Caml compiler: O'Caml distribution (Win32 port)

[This message was edited by Prototyped on January 21, 2002 at 04:14.]
 
D

Deleted member 5324

Guest
After looking at all the Scheme implementations, I'd have to say that MzScheme looks the best -- and it also includes very supportive development environment.

O'Caml only has one implementation :)

For Lisp, the best that I've seen isn't the one Prototyped linked to, though -- it's CMUCL (I don't have a link handy at the moment, but it's not too hard to find.) CMUCL compiles to native code and is (I think) a little more comforming to the standard than clisp is. I can't vouch for what it's like on Windows, though. While the best ML implementations are Free Software, Lisp isn't in the same situation -- the best Lisp implementations are all commercial (especially look into Allegro Common Lisp, which seems to be by far the best available, but it's quite expensive.)

In my opinion, the ML languages (and Haskell) are far more focused on functional programming than the Lisp languages (Scheme, Common Lisp, etc.) The ML languages (and Haskell; especially Haskell, even :)) are far more focused on immutable data structures, one of the very important building blocks of functional programming.

You see, the basis of functional programming is functions, and proper functions don't have side effects -- that's why immutable data structures are so important. Unfortunately, all data structures and variables in Lisp/Scheme are mutable. In ML/Haskell, however, variables can't be rebound, and only references are mutable (O'Caml allows mutable values in records too, but only if they're declared mutable.) Because of this fundamental difference, I think ML/Haskell teach functional programming much better than Lisp/Scheme.

Jeremy
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by jemfinch:
In my opinion, the ML languages (and Haskell) are far more focused on functional programming than the Lisp languages (Scheme, Common Lisp, etc.) The ML languages (and Haskell; especially Haskell, even :)) are _far_ more focused on immutable data structures, one of the very important building blocks of functional programming.

You see, the basis of functional programming is functions, and proper functions don't have side effects -- that's why immutable data structures are so important. Unfortunately, _all_ data structures and variables in Lisp/Scheme are mutable. In ML/Haskell, however, variables can't be rebound, and only references are mutable (O'Caml allows mutable values in records too, but only if they're declared mutable.) Because of this fundamental difference, I think ML/Haskell teach functional programming much better than Lisp/Scheme.

I know you qualified it with "my opinion", but you're wrong ;)

In ML, no variable or data structure is mutable unless declared so by the programmer or inferred so by the typechecker. In Scheme, no variable or data structure is mutable unless explicitly mutated by the programmer.

Both languages have impure features, and Scheme is by no means "more impure" than ML. The mechanism for indicating mutation is different: in statically typed ML, the mutable variables and data structures are exactly those declared so by the programmer or inferred so by the typechecker. In dynamically typed Scheme, the mutable variables and data structures are exactly those that get mutated during their lifetime.

The distinction is actually present in most sophisticated Scheme compilers. If a variable is not mutated in its scope, then it is immutable, and unboxed and copy propagated.

I think your intuition is something like this: "the set of mutated variables in a Scheme program is possibly the same as the set of all variables". That's true, it is possible, but not necessary. And the exact same thing is true for ML.

And, since the ML typechecker can infer reference types, even the annotations are often missing. What is the set of mutable variables in this function:

code:<HR>
Code:
fun (x, y) -> x := 5; y

Or this one:

code:<HR>
Code:
(lambda (x y) (set! x 5) y)

In Scheme, nobody ever forces a programmer to ever use the impure features (mutation and call/cc). My experience with a whole lot of ML and Scheme code is that programmers in Scheme do not use mutation more (or less) than in ML. And, you can often see code like this (courtesy of another thread here) from newbies in ML:

code:<HR>
Code:
fun foo (ll : int list ref) =
  let
   val i = ref 0
  in
   while !i < 5 do (
     ll := !i :: !ll;
     i := !i + 1
   );
   ()
  end

So, Scheme is no less suitable for learning or practicing purely functional programming, provided you have the discipline to avoid the impure features. Exactly the same with ML.

What is the best language for learning functional programming? Here's my opinion ;): the lambda-calculus. Since Scheme without mutation (or call/cc) is a call-by-value lambda calculus, I prefer it over ML without mutation, which is a Damas-Milner (polymorphically typed, but without polymorphic recursion) typed lambda-calculus.

I've studied and taught functional programming at a university often considered one of the spiritual homes of functional programming, and I prefer to start with the lambda-calculus and later explore the restriction of type systems. Mind you, I usually don't complain about type systems in general, they are excellent engineering tools. But when the stated goal is "to learn functional programming", the restrictions of the type system can sometimes be unacceptable.

(kg*m*m)/(s*s)
 

MrMills

Ars Praefectus
3,727
Moderator
Well ... I think I have a handle on Scheme now after reading all 143 pages of An Introduction to Scheme and its Implementation.

And ... um ... wow. I'm intrigued by the concept of a language where everything is a list, but all the parentheses REALLY get in the way of comprehension. And some of the identifiers chosen (particularly car and cdr) defy belief.

It just seems like the language is so homogeneous that it's difficult to pick up on visual cues. And I haven't seen an implementation with helpful syntax highlighting (DrScheme does parenthesis matching, which is FAR more necessary in Scheme than any other language I've ever seen, but that's it).

Hmm.

Anyone know offhand how Lisp/Scheme is for stuff like object-orientation? Will someone come up with a .NET module that supports it?
 
Originally posted by MrMills:
And ... um ... wow.
Heh. Yeah, that's a not atypical reaction.

The canonical Lisp editor is, of course, Emacs. The support for Lisp programming in Emacs is exceptionally strong, especially with the ILISP package. It looks like there is MzScheme support in ILISP, too. With a good editor, and with practice, you just learn not to see the parens at all.

Anyone know offhand how Lisp/Scheme is for stuff like object-orientation? Will someone come up with a .NET module that supports it?
I imagine that there'll be Scheme implementation for .NET sooner rather than later; whether any of the CL vendors produce a full-fledged Common Lisp for it, I couldn't say.

Common Lisp has a tremendous object system (CLOS), but there's nothing standardized for Scheme, AFAIK.

Peace,
(jfb)

PS: Welcome to the wonderful world of Lisp, btw.
 

MrMills

Ars Praefectus
3,727
Moderator
Thanks.

I'm just trying to broaden my horizons here; the thing is that I often don't really get into a language unless I can produce useful stuff with it. And by useful, I mean GUI apps that don't suck. That's why I'm interested in a .NET language module, because that could plug into .NET's WinForms and stuff somehow to create a good UI.

(DrScheme's UI is not really a ringing endorsement of its GUI toolkit.)
 

petrov

Ars Tribunus Militum
2,714
I was forced to use DrScheme for my intro CS class when I was an undergrad at Rice and it was pretty sucktacular. I don't know if they worked out all the bugs since, but it was pretty icky ~3 years ago. unfortunately, I don't know a smooth GUI IDE that's comparable to VS.NET, but I'm a hardcore emacs fan, and really haven't looked that hard. emacs is teh win.

--sam
 

uberfunk

Seniorius Lurkius
15
I heard an anecdote about Lisp's use of Lots of SIlly Parenthesis.
This guy wrote an Emacs Lisp module that, when you are editing Lisp code (Scheme, Common Lisp, Guile, etc.), it does not show the parenthesis. Accomplished Lisp programmers did not notice the difference.

The point is that after you've spent a few weeks with it, you don't notice the parenthesis. I can empirically say this is true. Even when I do see them, they really help me understand the code. That is, if I have Emacs handy. Without Emacs, editing Lisp is close to a fate worse than death. Paren matching is a must at least. I would not wish coding Lisp on anyone without a paren matching editor. I'll stop rambling now
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by MrMills:
And some of the identifiers chosen (particularly car and cdr) defy belief.

If you haven't figured it out already, "car" and "cdr" stand for "contents of the address register" and "contents of the decrement register" which relate to how pairs used to be implemented, long, long ago. (See "Why 'car' and 'cdr'...?".)

Note that "car" and "cdr" are ordinary procedures, so if it really bothers you, you basically can rename them however you want.

Anyone know offhand how Lisp/Scheme is for stuff like object-orientation?

A simple OO system can be implemented with message-passing and using procedures as objects.
 

MrMills

Ars Praefectus
3,727
Moderator
I found the IBM developerWorks introduction to Haskell (req. free registration) useful ... of course, this was after I read the Gentle Introduction to Haskell (a misnomer if ever there was one!).

I'll have to check out that Mondrian link, too.

Makes me wonder if functional programming is as appropriate for the 'application' aspects of programming as the 'engine' aspects ... where the application is a use of the engine, etc.
 

petrov

Ars Tribunus Militum
2,714
Originally posted by stickboy:
Quote: "Originally posted by MrMills:
And some of the identifiers chosen (particularly car and cdr) defy belief."

Note that "car" and "cdr" are ordinary procedures, so if it really bothers you, you basically can rename them however you want.

IIRC, DrScheme uses the synonyms of "first" and "rest", which could be implemented yourself by simply doing:
code:<HR>
Code:
(define first
  (lambda (l)
    (car l)))<P>(define rest
  (lambda (l)
    (cdr l)))

--sam
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by petrov:
<BLOCKQUOTE>code:<HR>
Code:
(define first
  (lambda (l)
    (car l)))<P>(define rest
  (lambda (l)
    (cdr l)))
<HR></BLOCKQUOTE>

The eta-expansion isn't necessary.

code:<HR>
Code:
(define first car)
(define rest cdr)

I prefer "car" and "cdr", because they're not loaded in the same way that "first" and "rest" are. The not-so-dirty secret about cons is that it doesn't construct a list, it constructs a pair. There's a Lisp convention (along with some builtin functions) that lists are implemented as pairs nested in their second component and terminated by null; but we could just as well choose to implement them as pairs nested in their first component and terminated by (current-input-port).

The good and bad thing about Lisp is that pairs are all you need. If you've got pairs, you can implement tuples of any length by nesting them. If you've got pairs, you can implement sum values as tagged pairs. If you've got products and sums, you can implement any algebraic datatype. If you've got that and tags, you can implement records. That and first class functions (and mutable state to make it easy) and you've got objects. All built with cons.

(kg*m*m)/(s*s)
 
Status
Not open for further replies.