;; C211
;; Lab 11 Notes
;; Sid Stamm
NOTE: I usually comment out my thoughts, but this time
I have to do some drawing, so I won't comment them out.
If you want to load any code, you will have to copy
and paste it.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PORTS and stuff...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
What the hell are ports?
Ports are basically a way to turn some sort of data that
may be complex or otherwise not easy to play with into
something managable and straightforward.
Why the word "port?" Well, frankly we call them ports
because it's a real-world metaphor to which most people
can relate.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Boats
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Boats? Sure, why not. Imagine you work at a shipyard.
You have to deal with all these boats out in the sea, and
somehow you need to be able to get things on and off the
boats. Not only that, but maybe if you're trafficking
something like illegal drugs or cars, you want to make sure
the movement of goods goes exactly as you want.
To do this, you need to control the inventory as it comes
on and off the boats.
Here's a rough picture of a shipyard:
(ocean)
+--o---o---o---o---o---o--+
| 1 2 3 4 5 6 | (ports)
+--^---^---^---^---^---^--+
(traffic control)
O
\+/
|
/ \
(you)
(warehouse)
/----\
------
| |
/------\
On one side you have the ocean (where the boats are) and
on the other there is your warehouse.
Say you want a ship to dock so you can get some drugs or
cars off the boat. You tell traffic control you want the
boat to dock, and it picks a free port and attaches your
desired boat to that dock:
+-+
| |
| |
V
+--o---o---o---o---o---o--+
| 1 2 3 4 5 6 | (ports)
+--^---^---^---^---^---^--+
As you can see, the port is narrow so you can only get
one thing off the boat at a time. Now that it is docked
at port 1, you can tell your minions to get stuff from
that port:
"Hey Bob, go see what's at port 1."
"Okay Sid."
So Bob goes to the port and isn't allowed onto the boat
but that's okay since he can "peek" in and see what's first
in line to come out. Bob sees what it is and comes back
to tell you.
"Hey, Sid, it was a Porsche."
"Okay, that means there are cars on that boat. Why
don't you go get the cars one at a time. If it is
a foreign car, put it in the warehouse, if it's a
domestic then push it into the ocean."
Bob goes back to the boat and takes the first car (which
was a Porsche) and puts it in the warehouse. He then
goes back to the boat repeatedly until there are no cars
left. He pushed a few into the ocean and put a few in the
warehouse, but there are no cars left on the boat. When he
goes back to the boat, this time instead of a car he is
given an End Of Freight slip.
"Hey, Sid, boat's empty. Here's the EOF."
"Thanks, now close the port so the boat can leave."
You leave and go back to the office. You hand your boss
the key and say:
"Hey Alex, here's the key to the warehouse. I filled
it with the cars you wanted."
"Thanks Sid. Go home."
You go home and that's the end of the story.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; What?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
You're probably wondering what that had to do with scheme.
It's quite simple really:
- The ocean is the set of all complex or weird objects (boats)
that you'd like to attach to a port so you can get stuff
out of them in an orderly fashion. Two examples covered
this week are Files and Strings.
- The shipyard (specifically traffic control) is scheme.
- The contents of the warehouse is some output you're
building.
- The cars are the items (characters or whatever) that you
are reading from the data blob (blob being a string
or file)
- You're the programmer (duh)
- Bob is the program you wrote to do all your work.
So when you ask traffic control (scheme) to dock the boat at
a port, it tells you which port it chose:
> (define myport (open-input-string "a bunch of cars"))
> myport
#
Then you can see what the first thing waiting at the port
happens to be:
> (peek-char myport)
#\a
Then go to that port to get stuff:
> (read-char myport)
#\a
> (read-char myport)
#\space
And so on until you read the end:
> (read-char myport)
#!eof
> (eof-object? (read-char myport))
#t
That's kind of like the "end of freight" slip that Bob
gave to you. Now there's an easy way to figure out when you
are done.
When you get these objects you can do whatever you want with
them: put them in a list, throw them into the ocean, etc.
When you're done with the port, you close it to let the boat
go away.
(close-input-port )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Translation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Okay, let's pretend that our boat is the following string:
> (define boat "ididddidiiiidid")
Now, Bob needs to get all the imports and put them in the
warehouse and throw all the domestics away (ignore them).
Here's a scheme program that basically does what Bob did:
(define do-work
(lambda (boat)
(let ([port (open-input-string boat)])
(if (char? (peek-char port)) ;;see if there are cars
(let bob ([warehouse '()]) ;;warehouse starts empty
(let ([next-car (read-char port)])
(if (eof-object? next-car)
(begin (close-input-port port)
warehouse) ;;return warehouse
(if (equal? #\i next-car)
(bob (cons next-car warehouse))
(bob warehouse))))))))) ;; throw it away
> (do-work boat)
(#\i #\i #\i #\i #\i #\i #\i #\i)
I'll be darned, the warehouse had a bunch of boats in it!
Notice how I determined that the end of the file showed up?
I used (eof-object?