I'm looking for some well written (and non-trivial) functional code to
study. I've seen and written enough C code that it's fairly easy for
me to write large programs without too much struggling. But when I try
to write lisp or ocaml code, everything seems to fall apart after a few
hundred lines. I can write short recursive functions like you do in
class. What I'm not sure how to do is structure code, move data
around, and debugging. (They never teach you that in school.) I
usually learn best using the study, copy, paste, change methodology.
Thanks!
········@signalsguru.net wrote:
> I'm looking for some well written (and non-trivial) functional code to
> study. I've seen and written enough C code that it's fairly easy for
> me to write large programs without too much struggling. But when I try
> to write lisp or ocaml code, everything seems to fall apart after a few
> hundred lines. I can write short recursive functions like you do in
> class. What I'm not sure how to do is structure code, move data
> around, and debugging. (They never teach you that in school.) I
> usually learn best using the study, copy, paste, change methodology.
> Thanks!
I think you should read HtDP:
http://www.htdp.org/
It teaches you how to decompose a problem into smaller programs and
solve these in a functional language (Scheme in this case).
········@signalsguru.net wrote:
> I'm looking for some well written (and non-trivial) functional code to
> study. I've seen and written enough C code that it's fairly easy for
> me to write large programs without too much struggling. But when I try
> to write lisp or ocaml code, everything seems to fall apart after a few
> hundred lines. I can write short recursive functions like you do in
> class. What I'm not sure how to do is structure code, move data
> around, and debugging. (They never teach you that in school.) I
> usually learn best using the study, copy, paste, change methodology.
> Thanks!
On common-lisp.net there are a plenty of projects with source code that
you can study. Also check out other repositories / link collections,
like cl-user.net or www.cliki.net.
Practical Common Lisp and Paradigms of Artificial Intelligence
Programming are two good books with non-trivial programs as examples.
Pascal
--
3rd European Lisp Workshop
July 3 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
········@signalsguru.net wrote:
> I'm looking for some well written (and non-trivial) functional code to
> study. I've seen and written enough C code that it's fairly easy for
> me to write large programs without too much struggling. But when I try
> to write lisp or ocaml code, everything seems to fall apart after a few
> hundred lines. I can write short recursive functions like you do in
> class. What I'm not sure how to do is structure code, move data
> around, and debugging. (They never teach you that in school.) I
> usually learn best using the study, copy, paste, change methodology.
> Thanks!
First of all, Common Lisp is not a purely-functional language;
everything[1] you can do in C you can do in Lisp with a simple, straight
forward translation.
Code organization is all about abstraction. Different methodologies
allow you to abstract different things -- in object oriented code you
abstract away what concrete code is actually being executed for any
particular function call, in functional programming you abstract away
specific parts of code that change from call to call.
The main tool for functional programming's abstraction is first class
functions, often as anonymous functions (lambda). For example, in Lisp
you can sort an array of numbers with (sort arr (lambda (a b) (< a b)).
Whenever you see two blocks of code that look the same, except for
some body, you should think about making the varying body a parameter to
some function.[2]
-- MJF
1] Well, everything that does not involve reinterpreting memory.
Unions and unsafe casts don't translate over.
2] Lisp also allows you to do macros to get the same effect. In Lisp
without macros, you could write C's while loop as:
(defun while-fn (test body)
(if test
(progn body (while test body))
nil))
Which would be used as:
(let ((x 0))
(while-fn (lambda () (< x 10))
(lambda () (print x) (incf x))))
With Lisp macros, you can hide the usage of lambda as well:
(defmacro while-mac (test &body body)
`(while-fn (lambda () ,test)
(lambda () ,@body)))
Which is used as:
(let ((x 0))
(while-mac (< x 10)
(print x) (incf x)))