Don't wanna be here? Send us removal request.
Text
Ch2
In chapter 2, we start learning about functions.
This is a good thing. Functions are really useful!
You can define them like so:
(define (name-of-function input1 input2) some-expression)
This will produce a function named name-of-function that takes input1 and input2 as inputs, and evaluates some-expression to produce its output.
Since everything is just an expression, you can also use the output of one function as an input for another.
in 2-5, we go over filesystem i/o a little bit. I made a function that reads and writes strings to a file, using read-file and write-file. There’s a mention of stdout, which isn’t really explained well. I probably should look into what it really means.
They also introduce big-bang, which has kind of defined a lot of what’s happening in ch 3 and 4.
big-bang is, for reasons that I do not understand, called a “world program” or something like that. It handles a WorldState, which seems to be a single piece of data, and uses it as input for any of the functions that are triggered within it, and takes the output of those functions as a new WorldState. Usually, you’ll also design something called a “renderer”, which will take the current WorldState and produce a racket “scene”, which is basically an x-px by y-px rectangle with images and such in it, much like a Processing canvas.
It has a framerate thing built in, where every frame, you can have it update stuff- i.e. refreshing frames to create animations, iterating a counter to change the WorldState, etc.
There are also functions that will take keyboard and mouse input.
0 notes
Text
A summary up to this point - ch 1
I haven’t made much progress over the past couple of weeks because of family stuff / probably being in my brain in weird ways, and I’m having trouble progressing right now.
I know that there are different things I can try in order to get myself out of this kind of slump, and I’m thinking that one of these things is gonna be review.
So here I am.
Chapter 1:
I learned about syntax, atomic data, and built-in expressions. We were introduced to numbers, strings, booleans, and images as forms of atomic data.
I’ve done a little bit of python before, so Dr Racket’s way of organizing information was a little bit novel for me- If you want to add numbers together, for instance, you’d do (+ 5 5), instead of 5 + 5.
I like this in some ways, since it lets you mash together a lot of data very quickly (+ 5 4 8 1 3) instead of 5 + 4 + 8 + 1 + 3, but that also seems kind of annoying if you’re trying to do different types of functions at the same time- consider (+ (* 5 7) (/ 9 (+ 3 6))).
It keeps your code organized effectively, but it feels harder to read. Maybe I’m just not used to it yet. Using a nice editor is definitely key- I really wouldn’t want to have to manually check if each parenthesis is closed or not.
It seems like Dr Racket is designed to keep a user from getting sloppy in a lot of ways. The syntax means that you’re aware of exactly what order you can expect expressions to evaluate, and typing seems to be pretty rigid.
For instance, in another language I might expect to be able to do the following
5 == 5 “test” == “test” true == true
but in Dr Racket, I’d have to do something like this:
(= 5 5) (string=? “test” “test”) (boolean=? #t #t)
Because of this, there’s less space for kludging and less ambiguity- I know Python is happy to interpret datatypes loosely, and that’s probably not what I actually want when I’m just learning to walk here.
We went over predicates, which seem really important, but I haven’t been using since the beginning. You can use them to check datatypes.
For instance, (string? “asdfa”) evaluates to #true, and (boolean? 27) will evaluate to #false.
0 notes
Text
Key points from ch 3
Information and data are two different things; information has context and exists in the “real” world, whereas data is a distillation thereof.
We’re probably talking something fairly similar to Johanna Drucker’s data/capta dichotomy, where data is information in its fullest, but capta is an inevitably reductive extraction of usable/legible (a la seeing like a state) stuff.
What happens is that, if you’re designing a program (or a government), you’re simply incapable of meaningfully engaging with the whole picture. Think Borges’s On Exactitude in Science, a story about a civilization that practiced cartography so exactingly that its greatest map could only contain its full detail by being created at the full size of the territory it described.
... In that Empire, the Art of Cartography attained such Perfection that the map of a single Province occupied the entirety of a City, and the map of the Empire, the entirety of a Province. In time, those Unconscionable Maps no longer satisfied, and the Cartographers Guilds struck a Map of the Empire whose size was that of the Empire, and which coincided point for point with it. The following Generations, who were not so fond of the Study of Cartography as their Forebears had been, saw that that vast map was Useless, and not without some Pitilessness was it, that they delivered it up to the Inclemencies of Sun and Winters. In the Deserts of the West, still today, there are Tattered Ruins of that Map, inhabited by Animals and Beggars; in all the Land there is no other Relic of the Disciplines of Geography.
So we simplify. We make things faster by discarding parts, even if some of those parts have meaning, or add context. We shape things to fit our needs and worldview in the way that we describe them.
But I digress. The idea is that info comes from the real world- an amount of watermelons, height of a horse, density of a poison gas cloud. Those are turned into data that a computer can process; an integer, a string, a boolean- and this data is processed by the computer, then we look at it again, and make it back into information. These numbers say that we have enough watermelons to feed a village, or that the poison cloud is dissipating quickly enough that we can avoid evacuating.
The domain of information generally consists of stuff outside of CS, whereas the realm of data is where the software engineers and developers lay their claim.
Since we’re designing the data that is extracted from information, we have to do so mindfully, and to document well. HTDP has a few suggestions on how to do this in a way that serves both users and other programmers who might have to tweak our code.
For constants, it’s recommended that we document the type and provide an interpretation. The example given is this:
(define TEMP 42) ; a TEMP is a number ; represents degrees Celcius
Fairly self-evident. We document functions in a similar way.
0 notes
Text
Explaining big-bang
HTDP Book 1, Ch 2.5 introduces big-bang, and it seems like it’s gonna be a fairly important thing. It’s certainly got a very important name.
However, they don’t do the best job of explaining it, so I’m gonna give it a shot.
Big Bang
Essentially, big bang is used to keep track of a value, and to listen for events that can change this value.
Why is this important?
If you want to be able to simulate something, you need to be able to keep track of what’s going on with the thing- if you want to simulate a ball, you might want to think about what its position is, for instance, and to be able to change that position based on whether someone decides to, say, kick it.
So how would you design this?
First, let’s write a function that draws a ball.
(require 2htdp/universe) (require 2htdp/image) ; these are the libraries required in order to use big-bang and images
(circle 5 “outline” “black”) ;please note that tumblr uses smart quotes, so you may have to delete and retype the ‘ “ ’ if you’re copying and pasting
Pretty simple stuff. Let’s place that circle into a scene
(require 2htdp/universe) (require 2htdp/image) ; these are the libraries required in order to use big-bang and images
(define (ball-in-scene y) (place-image (circle 5 “outline” “black) 50 y (empty-scene 100 400))) ; we’re defining it as a function so we can interactively alter “y” via big-bang
Not too bad so far. We’re putting a ball of radius 5 in a scene of width 100 and height 400, at location 50, y. This puts a ball in the middle of the scene at whatever height we want. (in order to keep things simple, we’re not gonna deal with 2 dimensional movement)
So here’s where big-bang comes in.
(require 2htdp/universe) (require 2htdp/image)
(define (ball-in-scene y) (place-image (circle 5 “outline” “black) 50 y (empty-scene 100 400)))
(big-bang 0 [to-draw ball-in-scene] )
Nothing fancy yet. to-draw is one component of big-bang that simply draws the current state of big-bang, using a function that produces graphics. In this case, our previously defined ball-in-scene is used with the initial value of big-bang, meaning it’s running (ball-in-scene 0) at first.
So let’s make this a little more interesting.
(require 2htdp/universe) (require 2htdp/image)
(define (ball-in-scene y) (place-image (circle 5 “outline” “black) 50 y (empty-scene 100 400)))
(big-bang 0 [to-draw ball-in-scene] [on-tick add1] )
If you remember animate from the image library, that’s essentially what we’ve reproduced here. on-tick counts each frame that passes, and triggers add1 on each frame. What does it add1 to? To the initial value, or to the 0 that we inserted into big-bang.
It basically runs big-bang again, but with a value of (add1 0), or 1. This means it triggers [to-draw ball-in-scene], but with a value of 1, or (ball-in-scene 1), which will draw our ball at location 50, 1.
On-tick happens again, and the whole process repeats itself until we stop the program. So it’ll be (ball-in-scene 0), then (ball-in-scene 1), then 2, 3, etc.
Almost all of the clauses that can be used with big-bang will affect the current value in some way. Let’s talk about on-key
on-key is a clause that takes a function as an argument, and runs that function when there’s a keypress.
Let’s say we want to reset the ball to the top of the screen when we press a key.
Almost all of the functions in big-bang basically take the current state of big-bang as an input, and output a new state for big-bang. So let’s write a function that takes the current state of big-bang and returns a nice big “0″ to it.
(define (reset s k) 0)
This will take 2 inputs and output 0. Why two inputs?
Well, we’re looking for two inputs because on-key requires a function that takes two inputs. This is because on-key expects to input both the current state of big-bang, and the key that is pressed into whatever function you’ve written. This is so that the function can distinguish different keys, and make different things happen when different keys are pressed. In this case, “s” is the current state of big-bang, and “k” is the key that is pressed.
So here’s what our program should look like once we’ve put everything together:
(require 2htdp/universe) (require 2htdp/image)
(define (ball-in-scene y) (place-image (circle 5 “outline” “black) 50 y (empty-scene 100 400)))
(define (reset s k) 0)
(big-bang 0 [to-draw ball-in-scene] [on-tick add1] [on-key reset] )
There’s one more part that I’ve learned so far, and that’s stop-when. This is a clause for big-bang that will terminate big-bang when its value is #true.
For instance, let’s have the program stop once the ball reaches all the way to the bottom.
(define (bottom y) (> y 400))
this function will check if the y value on the ball is greater than 400 (the height of the scene), and return #true if it is. We can use it as the argument for stop-when, and we’ll get the following:
(require 2htdp/universe) (require 2htdp/image) (define (ball-in-scene y) (place-image (circle 5 "outline" "black") 50 y (empty-scene 100 400))) (define (reset s k) 0) (define (bottom y) (> y 400) )
(big-bang 0 [to-draw ball-in-scene] [on-tick add1] [on-key reset] [stop-when bottom] )
Now, when the state of big-bang > 400 (aka the ball reaches the total height of the scene), it will trigger stop-when, and the computation will stop, leaving you with the last rendered frame.
that’s all i’ve got for big-bang right now.
0 notes
Text
big-bang
holy shit what the fuck dr racket has a function called “big-bang”, which is apparently called a world function.
Remarkable. I feel powerful. I feel like there’s some kind of magic to it.
0 notes
Text
HTDP Ch 1 test
Chapter 1. Arithmetic:
1. What characterizes atomic data? Name two types.
2. How would you write 5 * 3 + 12 * 7 in order to accurately evaluate it in DrRacket?
1.1
3. Name 3 operations that can be done on numbers
1.2: The Arithmetic of Strings
4. Why do most programming languages abstract from machine code?
5. write a function that concatenates two strings to produce the phrase “swag is for men”
1.3: Mixing it Up
6. take the string “123456″. What is the string length according to DrRacket’s built in function?
7. take the string “abcdef”. Write an expression using string-ith to produce the letter “d” from this string. bonus: what datatype is produced from this process?
8. evaluate the following expression: You may use a calculator for multiplication, addition, subtraction, etc, but you must evaluate the logic yourself. (string-ith (number->string (* (string-length “012345″) 135) ) 1)
9. consider the string “hello world”. a. Does (substring “hello world” 5) include (string-ith “hello world” 5)? b. Does (substring “hello world” 2 4) include (string-ith “hello world” 4)? b-2. Does it include (string-ith “hello world” 2)? c. What is the value of (string-length (substring “hello world” 2 4))? d. If string-ith 5 of a string is the last character, what is string-length of that string?
10. Which of the following are numbers? 42 “42″ (string->number “forty two”) (number->string 42)
1.4 Arithmetic of Images
11. Write a function that prints a snowman in a scene. Ensure that it includes a single variable that can be used to adjust the snowman’s size without affecting its proportions.
12. What are 3 functions that take an image as input?
1.5 The Arithmetic of Booleans
13. What values are valid for a boolean?
14. resolve the following expressions a. (or #true #false) b. (and #false #true) c. (not #false) d. (and (not #false) #true) e. (or (and #false (not #true)) #false)
1.6 Mixing it Up with Booleans
15. Evaluate the following statement: (if (= (string-length “537″) 3) “wow!” “oh no!”)
16. Write an expression that compares two integers and returns the larger of the two.
1.7 Predicates: Know thy Data
17. Write an expression that checks if its input is a number, and adds 5 to it if it is, or returns the same input if it isn’t.
Other
18. Write an expression using a conditional statement that tells you if its input is a string, a number, an image, or a boolean.
0 notes
Text
Day 2
input
3/4 mile run train ride SH -> Xi’an a big-ass dinner lotsa quality time sitting next to MZ on the train the rest of HTDP’s prologue city wall/gate + muslim street in Xi’an
output
(require 2htdp/image) (require 2htdp/universe)
(define sceneHeight 300) (define radius 10)
(define (drawBall y ) (place-image (circle radius "outline" "green") 100 y (empty-scene 200 sceneHeight)) )
(define (anim x) (cond [(<= (+ x radius) sceneHeight) (drawBall x)] [(> (+ x radius) sceneHeight) (drawBall (- sceneHeight radius))] ) )
(animate anim)
first bit of code ‘i’ve written in DrRacket. Has a ball of size radius fall through a scene of width 200 and height sceneHeight until it reaches the bottom, then stops. I want to figure out how to adjust the speed of the ball falling, but for some reason it wasn’t triggering the conditional for the ball to stop. I’ll figure it out later.
I didn’t run tonight, and I’m not sure I’ll be able to run tomorrow morning, so that most likely leaves tomorrow night. I have to figure out an appropriate route to get the distance I want and put me back at the hotel.
I saw a lot of wonderful stuff today, and felt weirdly proud about my heritage, but also- my ancestors weren’t the motherfuckers living in those castles. They were the ones killing themselves building it, laboring painfully to stack the bricks, or make them, or to farm the grain to feed the workers, or to cook the food they ate, or to clean the streets of debris. Just a thoguht.
0 notes
Text
Day 1
Input:
3x running laps (probably 3/4 of a mile) A tasty fucking dinner and a roll of cookies for desert HDTP Prologue How to Program Arithmetic and Arithmetic Inputs and Outputs
Output:
(require 2htdp/universe) (define (ballAnim height) (place-image (circle 10 100 "red") (+ 50 (* 40 (cos (/ height 20)) ) ) (+ (* (sin (/ height 10) ) 30) 50) (empty-scene 100 100))) (animate ballAnim)
Messing around with sine waves for smooth, hypnotic movement (maybe wanna figure out how to make a gif at some point). I think this is functional programming, maybe? Dunno. Syntax is really weird compared to the stuff I’ve tried before.
(function, arg1, arg2, arg n+1)
Definiining functions is weird too. ex:
(define (funcName funcArg) ( 12 + funcArg)) (funcName 5) = 17
Chapter’s longer than I thought. It might take two or three days to get through each one, in which case I might be looking at a 12wk timeframe instead of the 6 I’m aiming for, esp since this is just the intro- I imagine it might ramp up.
0 notes