PDA

View Full Version : "functional"...?


inkedmn
10-30-2002, 09:36 PM
ok, i'm interested in learning what "functional" programming is exactly. i know that some ML dialects are functional languages, and i know that functional languages don't (inherently) support OO.

at least, i THINK i know those things...

would somebody be able to tell me where i could find a good introduction to functional programming? (preferably langauage independent)

thanks :)

GnuVince
10-30-2002, 09:55 PM
http://www.cs.nott.ac.uk/~gmh//faq.html

That should answer all your questions.

scanez
10-30-2002, 10:05 PM
inkedmn: duder, I'm functional ;)

GnuVince: recommend a good LISP book? or at least online tutorial? :)

inkedmn
10-30-2002, 10:09 PM
ok, i think i want to give functional programming a go...

good starting language?

so far i'm looking at Haskell and Scheme, but i'm VERY open to other suggestions.

scanez
10-30-2002, 10:16 PM
I started with OCaml...with the help of our good ole' Vince here ;)

Now it's lisp time! w00t! ... Berkeley uses Scheme in their intro programming courses so that also sounds lke a good choice...

sans-hubris
10-30-2002, 10:23 PM
The best way to understand functional programming is to actually do it, because no one can completely agree on what it really is, but if they were shown how a particular language works, they can tell you whether or not it's functional.

About the only thing that's agreed on is that the functional part of functional languages (since a lot of them have some amount of non-functionality in them) have no side effects. This means that you don't have things like input/output, variables (once something has a value, you can't change that value), loops (since you can't have variables, however, recursion is extremely important in functional languages), and some other things that are considered side effects that I'm sure I'm forgetting at the moment.

However, a lot of functional languages do have various non-functional programming pieces, such as input/output, loops, and variables, but those languages make sure that you know that those pieces aren't functional, usually through syntax.

GnuVince
10-30-2002, 10:50 PM
scanez: I don't have any good Lisp online books, but I do have good Scheme ones:

http://www.htdp.org/
http://mitpress.mit.edu/sicp/full-text/book/book.html
http://www.scheme.com/tspl2d/

If you have Debian: apt-get install mzscheme

bwkaz
10-30-2002, 11:06 PM
There's an online version of Guy L. Steele's Common Lisp, the Language hosted at CMU, in .tgz'ed HTML format:

http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/lang/lisp/doc/cltl/cltl_ht.tgz

Although this may not be very good if you don't already understand Lisp. Which you seemingly don't... ;)

There's a good book by somebody that I don't remember, that we used last year in my Programming Languages class. ANSI Common Lisp (sorry about the underlining, I know, these aren't links even though they look like they are) was the name of it, and actually, now that I think about it, it might have been written by GLS as well... hmm. But in any case, it was more of a tutorial-type book than a reference on the language (which CLTL is).

jemfinch
10-31-2002, 12:14 AM
I love functional programming.

Suggesting a language, though, isn't an entirely trivial matter. There's a rift in the functional programming community over typing. You have the strictly typed functional languages (SML, Haskell, O'Caml, Miranda, Clean, etc.) and the dynamically typed functional languages (Scheme...and some others I'm surely forgetting) Strictly typed languages don't let programs that aren't provably typesafe compile or run; dynamically typed languages allow such programs to run until the type error is actually encountered.

Personally, I'm a fan of strictly typed languages. The compiler can enforce so many invariants that I can rest assured that if my code compiles, it'll run cleanly and without much chance of program-halting error. Additionally, I can use the type system to my advantage by encoding other invariants into types. Let me give an example. I started writing an IRC library in SML recently. In IRC you have netmasks, which are strings of the form "nick!user@host.domain.tld" In order to write a more typesafe program, I decided to encode that invariant (the form of netmasks) in a type. So in my IrcUtils module, I define an abstract type "netmask" to be equivalent structurally to strings. I made two functions, "stringToNetmask" which takes a string and returns a netmask option (SOME netmask if the string is a valid netmask, NONE if it isn't), and "netmaskToString" that takes a string and returns a netmask. Then I define several other functions such as "nick," "user," "host," and a few others that take netmasks and return strings representing those various parts of it. This accomplishes two purposes: first, you can't write a program that misuses netmasks or uses invalid netmasks. Second, the program doesn't have to check multiple times if a netmask is valid before performing an operation on it. The nick/user/host functions can assume that a netmask is valid because the typesystem enforces it (by only allowing strings to become netmasks by going through the stringToNetmask function, which does all the checking necessary).

Dynamically typed functional languages do have some advantages, though. They're generally much more flexible, and are reputed (though not in my experience) to be easier to prototype things in. One advantage you'll find in the s-expression (parenthetical) languages like Scheme is that they support macros, which let you create some very powerful syntactical constructs to help you write less code (and, as you'll soon find out in your reading, the less code you have to write, the less chance of error you'll have.) I've very little experience with the dynamically typed functional languages, though. I need the discipline of strict typing much more than I need the flexibility they provide.

Functional languages can also provide some very interesting OO capabilities; O'Caml, Haskell, and Dylan all do so. Haskell's typeclasses in particular I found quite interesting.

There's also the strict/lazy evaluation difference in functional languages -- you'll find most languages to be strictly evaluated (expressions are evaluated as they're encountered) instead of lazily evaluated (expressions are evaluated as they're needed), but a notable exception is Haskell.

If I were to suggest a functional language to you, I'd suggest either SML or Haskell -- probably more SML, since it'll get you introduced to functional programming without the added necessities of understanding lazy evaluation, monads, and typeclasses. I can help you with either, though, since I took the time to learn Haskell earlier this quarter.

Let me also note, however, that I don't consider Common Lisp to be any more functional than C++. It's a mostly-imperative language with support for functional programming, IMO. I wouldn't suggest using it for learning functional programming.

Jeremy

jemfinch
10-31-2002, 12:16 AM
Oh, I didn't mention it, but I'd suggest SML over O'Caml because SML focuses much more on the functional and typesafe aspects of programming than O'Caml does. O'Caml ends up as a very...muddled functional language. Aside from that, anyone used to Java (as you are) will probably find SML more aesthetically pleasing in its naming convention than O'Caml (SML's Int.toString vs. O'Caml string_of_int is an example of the difference).

Jeremy

inkedmn
10-31-2002, 02:41 PM
alright, so i've dl'd compilers for both Haskell (GHC) and SML(SML/NJ). as well as some introductory documentation for both languages...

i'll keep you posted :)

/me dives in

inkedmn
10-31-2002, 09:29 PM
well, i must say that (so far) SML seems to be the easier language to read/understand. since this is going to be my first attempt at functional programming, i'm thinking it'll be the language i go for...

jamessan
10-31-2002, 09:41 PM
Scheme is a fun language. I'm taking a class about it at school right now. It takes a little getting used to being that it is a recursive language.

inkedmn
10-31-2002, 09:44 PM
the language itself is recursive? how does that work?

Strike
10-31-2002, 10:09 PM
Originally posted by inkedmn
the language itself is recursive? how does that work?
It's not, really, it's just that functional languages in general rely more heavily on recursion than imperative/procedural languages.

inkedmn
10-31-2002, 10:10 PM
ok, that's what i'd thought/read

inkedmn
11-01-2002, 09:52 PM
btw, 'ML' = ?

Strike
11-01-2002, 11:08 PM
From the ML faq (http://www.faqs.org/faqs/meta-lang-faq/):


1. What is ML?

ML (which stands for Meta-Language) is a family of advanced programming
languages with [usually] functional control structures, strict semantics,
a strict polymorphic type system, and parametrized modules. It includes
Standard ML, Lazy ML, CAML, CAML Light, and various research languages.
Implementations are available on many platforms, including PCs, mainframes,
most models of workstation, multi-processors and supercomputers. ML has
many thousands of users, is taught at many universities (and is the first
programming language taught at some).

Bradmont
11-02-2002, 10:43 AM
Originally posted by bwkaz
There's an online version of Guy L. Steele's Common Lisp, the Language hosted at CMU, in .tgz'ed HTML format:

http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/lang/lisp/doc/cltl/cltl_ht.tgz

Although this may not be very good if you don't already understand Lisp. Which you seemingly don't... ;)

It's also apt-gettable.

apt-get install cltl

8) Debian 8)

inkedmn
11-04-2002, 12:12 AM
so i've decided to go for ocaml.

i've been reading through the docs and i like the structure of the language, as well as it's plentiful documentation; and i'm happy to say that i'm learning what phrases like "strictly typed" and "value binding" mean :)

anyway, thanks to everybody for your input. and i do hope that vince, jeremy, and file13 stay close to answer my forthcoming array of stupid questions :)