Introduction and Chapter 1

Started by
84 comments, last by bodchook 15 years, 4 months ago
Quote:Original post by Alpha_ProgDes
*** Source Snippet Removed ***
i take it "we" is basically a new object every time i redefine it. something like:
*** Source Snippet Removed ***

i'm using Dr. Scheme by the way.


I'm not 100% sure what you really mean. There are multiple ways to answer that question.


'define' (re)binds the identifier to a value. If you write this in Scheme:
(define x 5)(define (f) x)(f) -> 5(define x 6)(f)--> 6

However, you cannot use this to achieve runtime mutability: if you use 'define' in a new environment/scope, the old value will be restored:
(define x 5)(define (f)   (define x 6)   'done)x--> 5(f)--> donex--> 5

The 'define' in 'f' creates a new environment (scope limited to f's body), in which a new 'x' is created.

To show this is not a simple matter, consider the following O'Caml code:
let x = 5;;let f () = x;;f ();;--> 5let x = 6;;f ();;--> 5

Don't break your head over it for now, chapter 4 will make this more clear where you'll write your very own Scheme interpreter. I would suggest you don't think of 'define' as a way to redefine values; act as if it's only allowed to use one define per identifier per scope.
edit: I see that Scheme (DrScheme anyway) prevents you from 'defining' the same identifier twice in an environment which is not the top-level one, which is good.



There's also the "referential transparency" issue. In Scheme, everything works through pointers/references. If you were to work in C++ this way, you could have two different pointers to 5, without them being equal because they point to different memory cells:
int a = 5, b = 5;int* p = &aint* q = &bp != q

As far as I know, there is no way to make this kind of distinction in Scheme with integers. Some languages take this approach everywhere: if you have two data structures containing the same data, they are indiscernible from each other. Only "object value" counts in these languages. Other languages such as C/C++/java/C#/... take into account "object identity": objects can represent the same values, yet have different identities (hence you need to use .equals() instead of == in java, etc.)
Advertisement
I have a semantics question.
(define (c-gt x y)  (cond ((> x y) x)  // why is the value contained in a list?        (else y)))(define (i-gt x y)  (if (> x y)       x              // when this value is standalone?      y))

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
I have a semantics question.
*** Source Snippet Removed ***


COND may have an arbitrary number of consequent expressions/commands for any particular predicate. For example, (cond ((> x y) (draw x) x) (else (draw y) y)) is valid. To do the same with an IF statement one would need to wrap the sequence with BEGIN. I'm not sure what the motivation was for the COND syntax... I guess they felt it was clearer to wrap a whole predicate/consequent clause in parentheses than have everything lifted up a level and have the meaning of terms in COND expression depend on some index.
Okay Section 1.2 problem 1.10 is killing me!! I figured out the first two but I can't figure out (A 2 4). It goes 2 4 16, then 65536! What kinda wacked out function does that!!!!?????

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
Okay Section 1.2 problem 1.10 is killing me!! I figured out the first two but I can't figure out (A 2 4). It goes 2 4 16, then 65536! What kinda wacked out function does that!!!!?????


Since this hasn't much to do with functional programming, feel free to skip the exercise.

If you're interested in the solution: you know that

  • A(1,0) = 0

  • A(1,n) = 2n if n > 0.



If you use this in the expansion of A(2,n) you get
A(2, n) = A(1, A(2, n-1))        = 2^A(2, n-1)        = 2^A(1, A(2, n-2))        = 2^(2^A(2, n-2))        = 2^(2^(2^A(2, n-3)))        = ...


So, using Knuth's up-arrow notation, we get A(2, n) = 2 ^^ n for n > 0:

A(2, 1) = 2 ^^ 1 = 2
A(2, 2) = 2 ^^ 2 = 22 = 4
A(2, 3) = 2 ^^ 3 = 222 = 16
A(2, 4) = 2 ^^ 4 = 2222 = 65536
Alright I'm reading the notes and I came across this.
(define (square x)   (* x x))(define square (lambda (x)    (* x x)))

When will I have or even want to use the lambda expression over the shorter define expression? Or is it just syntactic sugar that I shouldn't worry about?

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
Alright I'm reading the notes and I came across this.
*** Source Snippet Removed ***
When will I have or even want to use the lambda expression over the shorter define expression? Or is it just syntactic sugar that I shouldn't worry about?
The first is sugar for the second. You would usually use the first form, but if you wanted to curry a function, for example, you would have to use the second form.
Quote:Original post by Rebooted
Quote:Original post by Alpha_ProgDes
Alright I'm reading the notes and I came across this.
*** Source Snippet Removed ***
When will I have or even want to use the lambda expression over the shorter define expression? Or is it just syntactic sugar that I shouldn't worry about?
The first is sugar for the second. You would usually use the first form, but if you wanted to curry a function, for example, you would have to use the second form.

Curry?

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
Quote:Original post by Rebooted
Quote:Original post by Alpha_ProgDes
Alright I'm reading the notes and I came across this.
*** Source Snippet Removed ***
When will I have or even want to use the lambda expression over the shorter define expression? Or is it just syntactic sugar that I shouldn't worry about?
The first is sugar for the second. You would usually use the first form, but if you wanted to curry a function, for example, you would have to use the second form.

Curry?
Currying comes up in one of Chapter 2's exercises. We covered it a little here.
Just a couple of questions to make sure I am understanding this correctly. Well more I am just after feedback on if the answers I have are correct or if I am on completely the wrong track.

Exercise 1.6: My understanding is that in this case you can substitute the 'new-if' for the standard 'if'. Am I missing something? My understanding is that if is a more limited version (special case) of 'cond' and it can only contain single expressions for the <consequent> and <alternative>.

Exercise 1.7: The reason the good-enough? test doesn't work very well for small values is because it stops too soon. For example 0.001 finds the sqrt to be 0.04124542607499115 when the actual sqrt is 0.03162277. This is because with small values the absolute difference is small but the difference relative to the size of the initial value is still quite big. So to correctly solve the above example you would need to use a smaller cutoff for example a difference of less than 0.000001.

The reason the good-enough? test doesn't work very well for large numbers is because if a number is so large that it is not possible to store the decimal place information then an infinite loop will result because the guess will never be close enough.

This topic is closed to new replies.

Advertisement