Log in / create account User page Discussion Edit History Go to the site toolbox

User:Sam Odio/SCIP in Arc

From BluWiki

Contents

[edit] Structure and Interpretation of Computer Programs

Download the FULL SICP TEXT here.

I've decided to work through SICP... from what I've heard, it's a great way to learn functional languages. I'll be working through each exercise in ARC, instead of the Scheme dialect of LISP taught in the book. I'm a beginner to both functional programming and ARC, so I have no idea if this is going to be feasible. I'll post results as I progress through the book.

Note, for the purposes of going through this book, I'm using Arc2 - which can be downloaded here. More installation instructions are here.

[edit] Chapter 1

[edit] Section 1.1

Introduction to functional programming.

[edit] Main topics:

  • Expressions - An expression evaluates for something (ie, if you enter 486 into the interpreter and hit return, the result, or evaluation, would be 486)
  • Compound expression - a combination of several expressions. (+ 20 5) is a compound expression of +, 20, and 5. Result is 25.
  • Combinations - a combination of several compound expressions.
  • Compound procedures, specifically recursion.

[edit] Differences in this section between Scheme & ARC

Define is not recognized. Instead:

  • Use the "=" operator when setting a variable
  • Use "def" to create a procedure.

For example, naming a variable:

scheme> (define a 3)
arc> (= a 3)

Creating a procedure (notice the difference when passing the procedure and its variables are passed to the define function):

scheme> (define (abs x)
    (if (< x 0)
        (- x)
        x))
arc> (def abs (x)
    (if (< x 0)
        (- x)
        x))

Another difference is that Arc uses t and nil for true and false, rather than #t and #f


[edit] Exercise 1.1

(converted to arc)

arc> 10

arc> (+ 5 3 4)

arc> (- 9 1)

arc> (/ 6 2)

arc> (+ (* 2 4) (- 4 6))

arc> (= a 3)

arc> (= b (+ a 1))

arc> (+ a b (* a b))

I'm not sure yet how to do this, since the = operator sets the variable equal (it's not a test of equality):

arc> (= a b) 


arc> (if (and (> b a) (< b (* a b)))
   b
   a)

arc> (cond 
   ((= a 4) 6)
   ((= b 4) (+ 6 7 a))
   (else 25))

arc> (+ 2 (if (> b a) b a))

Notice I used if instead of cond, and set the last condition to "t" for always true:

arc> (* (if 
    (> a b) a
    (< a b) b
    t -1)
(+ a 1))


[edit] Exercise 1.1 solutions
10

12

8

3

6

3

4

19 

Unknown

4

Unknown

6

16


[edit] Exercise 1.2 (converted to arc)

arc>  
arc> 
arc> 
arc> 
arc> 


[edit] Chapter 2

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 7 May 2008, at 21:34. - This page has been accessed 399 times.
Disclaimers - About BluWiki