Welcome, Guest. Please Login
 
  HomeHelpSearchLogin FAQ Radified Ghost.Classic Ghost.New Bootable CD Blog  
 
Pages: 1 2 
Send Topic Print
How to Design Programs (HtDP) & SICP (Read 22258 times)
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
How to Design Programs (HtDP) & SICP
Aug 4th, 2009 at 6:23pm
 
Have you heard of the term 'Contract' before? .. or is it limited to a thing with HtDP?

Quote:
The goal of designing a program is to create a mechanism that consumes and produces data. We therefore start every program development by giving the program a meaningful name and by stating what kind of information it consumes and produces. We call this a CONTRACT. Here is how we write down a contract for area-of-ring, one of our first programs:

;; area-of-ring : number number  ->  number

The semicolons indicate that this line is a COMMENT. The contract consists of two parts. The first, to the left of the colon, states the program's name. The second, to the right of the colon, specifies what kind of data the program consumes and what it produces; the inputs are separated from the output by an arrow.
 
WWW  
IP Logged
 

MrMagoo
Übermensch
*****
Offline


Resident Linux Guru

Posts: 1026
Phoenix, AZ (USA)


Back to top
Re: How to Design Programs (HtDP)
Reply #1 - Aug 4th, 2009 at 6:45pm
 
Rad wrote on Aug 4th, 2009 at 6:23pm:
Have you heard of the term 'Contract' before? .. or is it limited to a thing with HtDP?

I haven't heard the term used quite that way, but the concept is universal in programming. 

Many languages (C and Java, for ex.) require you to supply a 'prototype' for each function or class.  In other languages (such as Perl,) the prototype is optional.  This prototype tells the compiler what input your function/class requires and what type of output it will produce.  Your contract sounds similar to these prototypes.

Another example of this concept comes up regularly in SICP when they talk about managing large projects.  They talk about having a contract with other programmers.  You agree to give them certain pieces of information, and they need to return the result in a specific format.  How they get that result is completely up to them, and you care little about how they do it, as long as their contract is fulfilled.


Rad wrote on Aug 4th, 2009 at 6:23pm:
start every program development by giving the program a meaningful name

SICP makes a big deal out of naming things as well.  "Naming things gives you power over them."
 
WWW  
IP Logged
 
Spanky
Radmeister
**
Offline


Rad's non-Admin Test profile
in Seamonkey

Posts: 73
Same as Rad


Back to top
Re: How to Design Programs (HtDP)
Reply #2 - Aug 4th, 2009 at 6:55pm
 
MrMagoo wrote on Aug 4th, 2009 at 6:45pm:
How they get that result is completely up to them

In other words, no micro-managing.
 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #3 - Aug 4th, 2009 at 7:11pm
 
Can you elaborate on what you means by 'classes' .. with typical examples?

numbers? strings?
 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #4 - Aug 4th, 2009 at 7:27pm
 
Finished the first 2 sections .. of which there are 43. Guess that's a start.

http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-1.html#node_toc_start

How does that saying go? .. "the jounney of a thousand chapters begins ..."

I don't like that they block access to the answer key .. unless you're a teacher/instructor.

I'm fairly comfortable with their Dr. Scheme programming environment.

Also finished the first SICP video lecture. (3 lessons, 30 mins each).

http://video.google.ca/videosearch?q=Structure+and+Interpretation+of+Computer+Pr...

Found the vdo much more enjoyable.
 
WWW  
IP Logged
 
MrMagoo
Übermensch
*****
Offline


Resident Linux Guru

Posts: 1026
Phoenix, AZ (USA)


Back to top
Re: How to Design Programs (HtDP)
Reply #5 - Aug 4th, 2009 at 8:10pm
 
Rad wrote on Aug 4th, 2009 at 7:11pm:
Can you elaborate on what you means by 'classes' .. with typical examples?

Classes are related to objects.  Every object belongs to a class. 

I tried to write up an explanation for you, but it didn't turn out well.  The Pickaxe uses a jukebox throughout the book as an example, and they do a better job than I can explaining classes using the example of songs in a jukebox:

http://whytheluckystiff.net/ruby/pickaxe/html/tut_classes.html
 
WWW  
IP Logged
 

Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #6 - Aug 4th, 2009 at 8:19pm
 
Thanks. Will ck it out.

SICP.2b "The strategy of wishful thinking." That guy is funny.

"It's no problem at all .. assuming we have these clouds." =)

Absurdity is a great teaching tool. Activates the mind by shocking it.

I'm starting to get glimpses into the coolness of the course. How they relate ideas/concepts. Very nice.

Have you seen this:

http://neopythonic.blogspot.com/2009/05/so-you-want-to-learn-python.html

particularly the later comments.
 
WWW  
IP Logged
 
MrMagoo
Übermensch
*****
Offline


Resident Linux Guru

Posts: 1026
Phoenix, AZ (USA)


Back to top
Re: How to Design Programs (HtDP)
Reply #7 - Aug 4th, 2009 at 8:53pm
 
Rad wrote on Aug 4th, 2009 at 8:19pm:
SICP.2b "The strategy of wishful thinking." That guy is funny.

Ya, but it's actually a useful way to look at how to write a program, too.  You can't imagine how liberating it is to not worry about how that function is going to work when you decide you need it.  You just decide you need it and move on. 

It's also the introduction to the contracts I mentioned earlier.  Your wishful thought turns into a contract for another programmer (or the contract for a function you write later yourself.)
 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #8 - Aug 5th, 2009 at 12:01pm
 
MrMagoo wrote on Aug 4th, 2009 at 8:53pm:
can't imagine how liberating it is to not worry about 

too bad I can't use that same technique with my checking account.  Smiley
 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #9 - Aug 5th, 2009 at 12:22pm
 
Starting to see the virtues of modularity in programming .. as it applies to "black-box abstraction" .. in order "to supress detail" .. what I would call the "lego approach".

Little thing, but makes sense and seems to be a foundational concept.

Also clear that Scheme & Lisp are all about parentheses (parens). Interesting cuz I tend to think parenthetically, which you might infer from my writing on the home page. I actually make an effort to minimize their use. I frequently go back and edit-out many parens, as over-use can make reading stilted.

Interesting to contrast Scheme's use of parens with 'Reverse Polish notation' .. which I first encountered during my days in the Navy's Nuceal Power school (Orlando), where some there had calculators featuring such notation.

http://en.wikipedia.org/wiki/Reverse_Polish_notation

3 + 4 vs (+ 3 4) vs 3 4 +

I like the way it bends your mind a different way .. forces it to think differently .. seeing things from a different perspective.

[ I also tend to think in 'hyperlinks' (particularly when comminicating) .. which is why I find the web such a 'natural' form of communication .. conceptually speaking .. and why I use so many (hyperlinks). ]
 
WWW  
IP Logged
 
MrMagoo
Übermensch
*****
Offline


Resident Linux Guru

Posts: 1026
Phoenix, AZ (USA)


Back to top
Re: How to Design Programs (HtDP)
Reply #10 - Aug 5th, 2009 at 4:12pm
 
Rad wrote on Aug 5th, 2009 at 12:01pm:
too bad I can't use that same technique with my checking account.

I think some people try.
 
WWW  
IP Logged
 

Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #11 - Aug 6th, 2009 at 12:46pm
 
do you happen to know (Scheme) whether helper/auxilary functions need to be listed FIRST?

the text lists them AFTER the main/dependant functions.

it would seem (to me) that the helper functions need to be listed first .. at least logically speaking. maybe not.

In other words, you can't determine PROFIT without first defining/determining REVENUE & COST .. no? Copy-n-paste from text:

Code:
;; How to design a program 
(define (profit ticket-price)
  (- (revenue ticket-price)
     (cost ticket-price)))

(define (revenue ticket-price)
  (*  (attendees ticket-price) ticket-price))

(define (cost ticket-price)
  (+ 180
     (* .04 (attendees ticket-price))))

(define (attendees ticket-price)
  (+ 120
     (* (/ 15 .10) (- 5.00 ticket-price))))

I know the dependent functions must be INCLUDED .. cuz I tried deleting them in the last example work-up, and the main functions failed to work. (as you might expect)
 
WWW  
IP Logged
 
MrMagoo
Übermensch
*****
Offline


Resident Linux Guru

Posts: 1026
Phoenix, AZ (USA)


Back to top
Re: How to Design Programs (HtDP)
Reply #12 - Aug 6th, 2009 at 3:38pm
 
Rad wrote on Aug 6th, 2009 at 12:46pm:
do you happen to know (Scheme) whether helper/auxilary functions need to be listed FIRST?

I don't know about in scheme.  I've never actually tried to compile a Scheme program.  I think they can be listed after.  In Perl they can be after, but in Ruby they have to be declared before they can be used.

Quote:
# SICP = Structure & Interpretation of Computer Languages. It's based on Lisp.
# HtDP = How to Design Programs. It used Scheme.


My understanding was that SICP also uses Scheme (and that Scheme was, in fact, written specifically for SICP.)


Hmmm, neither the MIT group page nor the Wikipedia page for Scheme mention SICP, but they do both say that Scheme was written by Sussman at MIT.

http://en.wikipedia.org/wiki/Scheme_(programming_language)
http://groups.csail.mit.edu/mac/projects/scheme/

I can't find the post, but I'm pretty sure Nigel said Scheme was written for SICP.

 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #13 - Aug 7th, 2009 at 1:57pm
 
MrMagoo wrote on Aug 6th, 2009 at 3:38pm:
pretty sure Nigel said Scheme was written for SICP

In the video, Abelson says, "We're going to conjure our spirits in a magical language called Lisp..."

Doesn't really matter, tho, since Lisp & Scheme (from what I've seen) are very similar .. at least in syntax (parenthetical).

From Nigel:

Quote:
*** do you happen to know (Scheme) whether helper/auxilary functions need to be listed FIRST?

They very definitely can be listed in any order; that is an explicit design choice by the language designers. This is something that HtDP doesn't try too hard to explain, by virtue of being a gentle introduction to the language rather than a more rigorous one. Understanding why this is so is something that comes to the fore more in SICP, since one of the things it is about is that the Scheme environment itself is a program that can be defined in Scheme.

*** the text lists them AFTER the main/dependant functions.

That is because it is trying to explain how programs can be created in what is called a "top down" fashion. That is, you start with the initial problem, and then solve it by breaking it into subproblems.

That's not the only way to write programs, or to read them, but it's a method of systematically showing how program design can proceed by starting with an initial problem statement, and reaching the final details as the last step.

*** it would seem (to me) that the helper functions need to be listed first .. at least logically speaking. maybe not.

No. It happens that many computer languages *do* tend to require things to be written in the other order, aka "bottom up" by starting with the detail and finishing with the pieces that tie it all together.

However, this is typically only true of programs which are "compiled" - that is, written once and then converted into machine language but which don't change. This is just something that suits the compilers and makes them simpler, and in particular means that they always know exactly what something means right away without needing to wait for it to be defined later.

Lisp-like languages have a different philosophy; for instance, they allow almost anything to be changed by supplying a new definition later on (which can include parts of the Lisp environment itself). Supplying definitions after they are used is no different than what happens when you change your mind about how one of the sub-parts of the program works, and providing a new definition that replaces the old one.

*** In other words, you can't determine PROFIT without first defining/determining REVENUE & COST .. no? Copy-n-paste from text:

You couldn't *run* the PROFIT program until you did determine those things - it would fail, because you hadn't defined the things you depend on. However, as long as definitions exist when you do ask it to run PROFIT, the system will be happy.

[ Technically speaking, this freedom to define things later only applies to what are called top-level definitions. Interior definitions inside a particular subprogram do have to be defined before you use them, and so it does tend to be that expert programmers lay out their programs in files in a bottom-up fashion. However, that's not really the kind of detail that matters to a text like HtDP. ]

- Nigel

Nigel rocks.
 
WWW  
IP Logged
 
Rad
Radministrator
*****
Offline


Sufferin' succotash

Posts: 4090
Newport Beach, California


Back to top
Re: How to Design Programs (HtDP)
Reply #14 - Aug 7th, 2009 at 8:06pm
 
Here's a question I sent Nigel:

re:

http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-5.html

first they define "area-of-disk" with variable "r"

but then they apply "area-of-disk" to the "area-of-ring" example,

which subtracts inner from outer areas, and uses 'outer' and 'inner' as variables.

BUT! the area-of-disk was programmed with the variable "r"

so i don't see how it can work when these variables are CHANGED to outer and inner.

because the area-of-ring program (variables = outer, inner) is based on area-of-disk program (variable = r)

See my point?

I mean, it works .. cuz I ran it. But I don't see how it should.

Code:
(define (area-of-disk r) 
(* 3.14 (* r r)))


Code:
(define (area-of-ring outer inner) 
(- (area-of-disk outer)
(area-of-disk inner)))


see how they change the variable for the area-of-disk prgm?

Also is Scheme a compiled language?

I am not compiling .. just clicking the "Run" button.
 
WWW  
IP Logged
 
Pages: 1 2 
Send Topic Print