Log in

24 August 2008 @ 08:53 pm
Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:

(define (new-if predicate then-clause else-clause)
  (cond (predicate then-clause)
        (else else-clause)))

Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)


(new-if (= 1 1) 0 5)


Delighted, Alyssa uses new-if to rewrite the square-root program:

(define (sqrt-iter guess x)
  (new-if (good-enough? guess x)
          (sqrt-iter (improve guess x)

What happens when Alyssa attempts to use this to compute square roots? Explain.

As per Exercise 1.5, this will result in an infinite loop (or, more precisely, you run out of stack space). Why does an infinite loop occur in the sqrt-iter, but not in the first two examples? This is simply a matter of applicative-order being a problem. Since in the first two cases both the consequent and the alternative can be fully evaluated, new-if happily evaluates them, and then evaluates the if of the predicate, returning the appropriate result. In the sqrt-iter case, however, the alternative is evaluated. Normally this is not a problem since the recursion is short circuited by the base case. However since the alternative is ALWAYS evaluated, regardless of whether or not it is the appropriate return of new-if, an infinite loop results.
24 August 2008 @ 08:38 pm
Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y)
  (if (= x 0)

Then he evaluates the expression

(test 0 (p))

In order to answer this question, we note that by applicative-order, we mean that both the operator and the operands (if applicable) will be evaluated before the function is applied. Conversely, by normal-order we mean that the interpreter will first expand the expressions (from left to right) down to primitive elements before applying the function.

Therefore, if Ben uses an interpreter which uses applicative-order evaluation, he will run into an infinite loop. This is due to the fact that the interpreter will evaluate (p), which will in turn evaluate to (p), which in turn evaluates to (p), which in turn evaluates to (p), ... ad infinitum.

If Ben's interpreter uses normal-order evaluation, however, test is first expanded out to the
corresponding if statement. The if statement is then evaluated (after substituting 0 for x), and 0 will be returned.

Thus as two different behaviors are expected for the different types of interpreters, we see that Ben will in fact be able to determine how the interpreter does evaluation.
24 August 2008 @ 08:27 pm
Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

First we note that when trying to evaluate this function, Scheme will first evaluate the operator, which in this case is an if-statement. The behavior of the if-statement can be broken down into two cases, namely:

  1. b > 0

  2. b <= 0

In the first case, the function will evaluate the if-statement to be +. Since b is positive, the result of the computation (i.e. a+b) will be the sum of a, and a positive value b.

In the second case, we note the if-statement will evaluate to -. We also note that b is negative, and thus know that there exists a c such that b=-c for c>=0. Continuing we see that the function evaluated is a-b=a-(-c)=a+c.

Since in either case a is added to the absolute value of b, we see that the function computes exactly what its name implies. More importantly this is accomplished by using the fact that the operator can be an expression, which in this case returns + or - (and thus addition of subtraction is performed, respectively), since Scheme will evaluate the operator before attempting to apply it.
24 August 2008 @ 08:18 pm
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

(define (sum-of-larger-squares x y z)
  (define (sum-of-squares x y) (+ (* x x) (* y y)))
  (cond ((<= x y z) (sum-of-squares y z))
        ((<= y x z) (sum-of-squares x z))
        (else       (sum-of-squares x z))))
24 August 2008 @ 08:13 pm
Translate the following expression into prefix form:

(/ (+ 5
      (- 2
         (- 3
            (+ 6
               (/ 4 5)))))
   (* 3
      (- 6 2)
      (- 2 7)))
24 August 2008 @ 07:52 pm
=> 10


=> (+ 5 3 4)


=> (- 9 1)


=> (/ 6 2)


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


=> (define a 3)


=> (define b (+ a 1))


=> (+ a b (* a b))


=> (= a b)


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


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


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


=> (* (cond ((> a b) a)
            ((< a b) b)
            (else -1))
      (+ a 1))

24 August 2008 @ 07:51 pm
H'Ok, so, I've started doing sicp. I suppose I should therefore do some cool things like writing down my solutions for saving in case they get lost and/or I want to reference them in the future. So I shall do that!

Chapter 1

1.1, 1.2, 1.3, 1.4
28 April 2008 @ 01:10 am
So on Saturday I was dicking around on my linux box pretending to work on my thesis when I decided that NOW would be a good time to do a "sudo apt-get upgrade." So I wait the 10 minutes for it to finish, and then go to install some other packages that I needed (I wanted to install a SOCKS server). So I type the "sudo apt-get" command, and the fucking server's like: "sorry, hostname is not found."

Now I'm thinking this is pretty fucking weird, so I type "hostname," and sure enough, my hostname comes up. At this point I'm hoping it's really something stupid, but am guessing it's not. So I go into my /etc/hosts file, and, sure enough, my hostname has been deleted from the loopback address. So I log onto irc to get some advice from some people in the support channel. I talk to this guy who's really nice, who basically says "yeah, that sucks, in order to fix it you need to have console (read: physical) access to the box so that you can reboot into recovery mode to fix the problem."

At the time I was 30 minutes away from the box, and really cba to drive all the way there to fix a stupid problem when I really didn't need the SOCKS server. So as I'm trying to convince myself that this was the case, I had an epiphany. "Hey Ryan," I thought, "you have access to a bash shell. Surely there's a way to exploit the box to get a root shell." At that moment I remember the "jessica_biel_naked_in_my_bed.c" or whatever it was called vmsplice exploit, and decided to pray that the upgrade didn't patch that vulnerability.

So I download the code, compile and run it, and, sure enough I get a root shell. I go into my /etc/hosts file and add in the appropriate line, exit out of the root prompt, and delete the exploit, now the proud owner of a fixed box.

I guess the lesson to be learned here is: linux is fucking retarded. Why the fuck would upgrading the system change the /etc/hosts file? There is ZERO reason for the system to to that. And people wonder why linux isn't mainstream.......
18 December 2007 @ 12:05 am
I'm not sure what's going on, but it looks like some of my recommenders decided that they didn't really want to write all the letters that I asked them to. This wouldn't have been a big deal if they didn't promise me that they were going to do it again and again. I really have no way of expressing in words how FUCKING PISSED OFF I AM RIGHT NOW. I can't FUCKING BELIEVE I spent all that FUCKING TIME doing those FUCKING APPLICATIONS all for some people to just say "oh well, whatever, no big deal I'll do it later. What? Time's up? Oh well, better luck next time." FUCKING SERIOUSLY. Fuck everyone. I don't give a flying fuck if you had anything to do with it or not. FUCK YOU. FUCKING SIGH. Why do I bother trusting people I know I shouldn't. Why? It's not like this is the first time this has happened, I should fucking know better by now. Might as well start looking for jobs or something. It will probably be better for me in the long run anyway, but even my ex-employer doesn't seem to want to hire me. Fucking sigh. Just fucking sigh.

I suppose I should thank the people who actually did try to help me, so thanks.
14 December 2007 @ 12:30 am
Ok, so my Aunt works as a recruiter, and as such she gets a lot of resumes. Currently, they're all over the place on her hdd, and whenever she needs to find someone's resume she needs to search for it. This sucks, but I told her to use folders, and basically showed her how. This is when the real complaint set in. Whenever she gets a person's resume, she sends it to multiple companies who are looking to hire people. The problem is, she sometimes needs to quickly come up with to which companies she sent a specific resume, and she has no real good way to do this. I was thinking about telling her to use a spread sheet, but it seems like there should be a better way. I was thinking about something like tagging files on the hdd with companies that could be easily implemented, or writing a small program that organizes the stuff for her. To be honest I don't know if anything like this exists, and if so, what it looks like. IF anyone has any ideas, let me know.