Any C-style function-based programs I've written tend to be a bunch of
functions composed of some other lower-level functions (which
themselves are likely composed of still more lower-level ones), plus
global variables (which seem to often be difficult to avoid). These
programs tend to grow global variables until things get too hairy, at
which point the program starts getting split off into smaller parts.
How does program design work in a language like Scheme? Which parts
should I write first (and last)? Can anyone sum up in a paragraph or
so how you go about writing a program in Scheme?
The one-paragraph answer:
In Scheme, you can go about writing a program in any way you want.
Let's explain:
Scheme, as a lisp, is a multi-paradigm meta-programming programming
language. You can define data structures, so you have data
abstraction. You have first class functions so you have functional
abstraction. You have a homoiconic language so you have easy
meta-linguistic abstraction. And you have macros so you have
syntactic abstraction. This allows you to write in any programming
style, using any paradigm, and even using different paradigms in
different parts of your program.
You can easily do object-oriented, functional, logic, imperative,
rule-based, event-based, data-driven, whatever, programming.
The language doesn't impose a specific methodology or paradigm, or
render your life difficult when you choose your prefered paradigm.
(eg. doing OO in C is possible, but difficult, until you skip a
metalinguistic level implementing a front-end to get Objective-C or
C++ ; on the other hand, doing OO in Scheme is trivial, there are
several libraries implementing different OO systems, and you can write
your own basic OO system in one page of scheme code, completely
integrated with the rest of the language).
So I would say that program design in scheme works by ignoring
entirely the language side, and concentrating only on the problem
domain: you will design your program in function of the problem you
have. If your problem can be solved correctly in some way, then you
will design your program that way. Scheme won't get in your way.
You can write any part in any order. You can do top-down or
bottom-up, or inside-out, or whatever.
--
__Pascal Bourguignon__
Thank you Pascal, but unfortunately I don't yet know what terms like
homoiconic and meta-linguistic mean. I'm new to Scheme and to
functional programming in general and would like to learn functional
programming using Scheme. Can you recommend a programming methodology
that would work well for that?
> Thank you Pascal, but unfortunately I don't yet know what terms like
> homoiconic and meta-linguistic mean. I'm new to Scheme and to
> functional programming in general and would like to learn functional
> programming using Scheme. Can you recommend a programming methodology
> that would work well for that?
--PR
--
Prabhakar Ragde, Professor plragde at uwaterloo dot ca
Cheriton School of Computer Science http://www.cs.uwaterloo.ca/~plragde
Faculty of Mathematics DC 1314, (519)888-4567,x34660
University of Waterloo Waterloo, Ontario CANADA N2L 3G1
The functional programming methodology.
Basically, you avoid side effects and assignments.
For example to compute the average of a list of numbers you could
write it procedurally:
(define (average numbers)
(let ((sum 0)
(count 0))
(do ((nums numbers (cdr nums)))
((null? nums)
(if (= 0 count)
'not-enough-numbers
(/ sum count)))
(set! sum (+ sum (car nums)))
(set! count (+ count 1)))))
or you could write it functionnaly:
(define (average numbers)
(letrec ((average-step
(lambda (numbers sum count)
(if (null? numbers)
(if (= 0 count)
'not-enough-numbers
(/ sum count))
(average-1 (cdr numbers) (+ sum (car numbers)) (+ count 1))))))
(average-step numbers 0 0)))
Start reading
http://en.wikipedia.org/wiki/Functional_programming
and follow the links.
Read SICP, Structure and Interpretation of Computer Programs.
--
__Pascal Bourguignon__
There is a six part section of my "Adventures of a Pythonista
in Schemeland" about functional programming that you may
find useful:
http://www.artima.com/weblogs/viewpost.jsp?thread=251474
> Can you recommend a programming methodology
> that would work well for that?
Yes, take a simple example of an imperative program
and try to translate it into a functional style, asking
here when you reach a point where you cannot progress.
Michele Simionato
>
> Read SICP, Structure and Interpretation of Computer Programs.
>
Does SICP show how to perform common tasks in a functional way? Is
there a design patterns book for FP. I remember that OOP finally
clicked after reading the GoF book. Currently, I understand what FP
is, but how do you deal for instance with GUI interaction which is
full of state? By means of Continuations Passing Style?
Thanks
Start with _How to Design Programs_.
[how do you design scheme programs]
> > The one-paragraph answer:
>
> > In Scheme, you can go about writing a program in any way you want.
>
> > Let's explain: [snip]
>
> Thank you Pascal, but unfortunately I don't yet know what terms like
> homoiconic
http://en.wikipedia.org/wiki/Homoiconicity
"[H] is a property of some programming languages, in which the
primary
representation of programs is also a data structure in a primitive
type
of the language itself, from homo meaning the same and icon meaning
representation."
It's all that a-lisp-program-is-a-list stuff
> and meta-linguistic mean.
http://en.wikipedia.org/wiki/Metalinguistic_abstraction
"[MLA] is the process of solving complex problems by creating a new
language or vocabulary to better understand the problem space."
<snip>
Try designing the same way you design C programs (and try to avoid
global variables).
Then, if you like, you can gradually move to the functional paradigm
(no variable mutations).
SICP will teach you how to program. It does this using many paradigms,
but a large portion of the book is what people would call "functional"
style. In any case, the book focuses more on teaching you how to
program, and I think it does a good job of illustrating the errant --
IMO -- emphasis on using one paradigm over the other.
In any case, definitely read the book, because it will be very
educational to you, and you will learn a good deal about programming
paradigms.
--
Aaron W. Hsu <arc...@sacrideo.us> | <http://www.sacrideo.us>
"Government is the great fiction, through which everybody endeavors to
live at the expense of everybody else." -- Frederic Bastiat
+++++++++++++++ ((lambda (x) (x x)) (lambda (x) (x x))) ++++++++++++++
Do you have any favorite multi-paradigm books on learning how to
program?
Without a doubt SICP is one of my favorites for its general
applicability and no nonsense approach. Other than this, I have to admit
that I haven't seen a lot of books that cover multi-paradigms that well.
The algorithm books do a good job of teaching a key part of programming,
and they really help with "thinking" about the problems in ways that you
wouldn't have thought of before, but often they are written in a sort of
imperative pseudo code.
Most of my computer books that I have are language references,
Algorithms related books, and then Mathematics texts. Of course SICP is
the major exception there.
"Concepts, Techniques, and Models of Computer Programming" by Peter Van
Roy and Seif Haridi
http://www.info.ucl.ac.be/~pvr/book.html
Michael
--
Michael Schuerig
mailto:mic...@schuerig.de
http://www.schuerig.de/michael/
For example, you can have embedded language for describing UI which
works via FFI with existing GUI library or you can have full blown UI
DSL which can be compiled to HTML, Java, c#, c, c++ at the same time.
Note that Scheme is probably the best option for a language-oriented
programming.
--
Bogdan
This is my favorite multi-paradigm book as well.