RDrama's Official Programmer Socks Reading Group

The Official RDrama Computer Science Reading Group

My dear !codecels, hello and welcome to the first meeting of RDrama's Computer Science Reading Group! Here's the idea - we (read: I) pick a computer science textbook, then post a list of sections and exercises from that textbook each week. In the thread, feel free ask questions, post solutions, and bully people for asking stupid questions or posting stupid solutions. If you don't want to read along, I'll post the complete exercises in the OP, so you can solve them without needing to read the book.

SICP

The book I'm starting with is 'the Structure and Interpretation of Computer Programs' (abbreviated SICP). It's a software engineering textbook written by Gerald Jay Sussman and Hal Abelson from MIT. The book builds programming from the ground up: starting with a very simple dialect of Scheme and growing it into a language with lazy evaluation, object-orientation and a self-hosting compiler. It's a fun book: the exercises are hands-on and interesting, the writing is informative without being droll, and both the book itself and a corresponding lecture series (complete with a 80s synth rendition of 'Also Sprach Zarathustra') are available for free online.

Languages

The book uses (a small subset of) Scheme as its primary language, but feel free to try using a different language. The book's dialect of scheme is available through Racket, but most lisps will work with only minor changes. Other dynamically-typed, garbage-collected languages with higher-order functions will also not require much hacking: there is an edition written in JavaScript :marseywebshit:, as well as a partial adaptation to python :marseysnek:. High-level, statically typed languages might also work: Java/Kotlin/C# :marseytunaktunak: seem doable, but I don't know those languages well. Strongly typed languages like Haskell will require some real hacks, and I'd avoid doing it in C, C++ or Rust.

Exercises

The book is split into five chapters:

  • Building Abstractions with Procedures
  • Building Abstractions with Data
  • Modularity, Objects and State
  • Metalinguistic Abstraction
  • Computing with Register Machines

This week, I'll be posting exercises from the first chapter. The chapter is pretty easy for those familiar with programming already, so I just want to get it out of the way. Here are the selected exercises:

Exercise 1.8

Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value (x/y² + 2y) / 3. Use this formula to implement a cube-root procedure which is wrong by at most 0.01.

Exercise 1.12

The following pattern of numbers is called Pascal's Triangle.

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
   ...

The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a procedure that computes elements of Pascal's triangle.

Exercise 1.18

Devise a procedure generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.

Exercise 1.31

Write a procedure called product that returns the product of the values of a function at points over a given range (product(l, r,step,f) = f(l) * f(l+step) * f(l + 2 * step) * ... * f(r)). Show how to define factorial in terms of product. Also use product to compute approximations to using the formula π/4 = (2 * 4 * 4 * 6 * 6 * 8 ...) / (3 * 3 * 5 * 5 * 7 * 7 ...)

Exercise 1.43

If f is a numerical function and n is a positive integer, then we can form the nth repeated application of f, which is defined to be the function whose value at x is f(f(...(f(x))...)). For example, if f is the function x → x + 1, then the nth repeated application of f is the function x → x + n. If f is the operation of squaring a number, then the nth repeated application of f is the function that raises its argument to the 2 * nth power. Write a procedure that takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the nth repeated application of f. Your procedure should be able to be used as follows: repeated(square,2)(5) = 625

Have fun! :marseytype:

35
Jump in the discussion.

No email address required.

!commenters - it seems like people didn't really care about this thread, so I don't think I'm going to continue it. I had hoped it'd be like the advent of code threads on /g/, but it seems like there aren't enough people here to make the thread more than me shouting into the void. :marseyshrug:

Jump in the discussion.

No email address required.

advent of code is masturbatory and only attracts the worst people.

Jump in the discussion.

No email address required.

>advent of code is masturbatory and only attracts the worst people

Where do you think we are?

Jump in the discussion.

No email address required.

:marseythonk:

post blahaj and socks

Jump in the discussion.

No email address required.

Here are my answers in Racket. Last two questions were the toughest. Might be worth to split future chapters into 2-3 weeks.

(define (cube-iter guess x)
  (if (good-cube-enough? guess x)
      (exact->inexact guess)
      (cube-iter (improve guess x) x)))

(define (improve guess x)
  (/ (+ (/ x (* guess guess)) (* 2 guess)) 3))

(define (good-cube-enough? guess x)
  (< (abs (- (* guess guess guess) x)) 0.001))

;;;;;;;

(define (pascal-triangle a b)
  (cond [(= a 0) 1]
        [(= a b) 1]
        [(<= a 0) 0]
        [(<= b 0) 0]
        [else (+ (pascal-triangle (- a 1) (- b 1))
                 (pascal-triangle a (- b 1)))]
        ))

;;;;;;;;;;

(define (itmul a b acc)
  (cond [(zero? a) 0]
        [(zero? b) 0]
        [(= 1 a) (+ b acc)]
        [(= 1 b) a]
        [(even? a) (itmul (/ a 2) (* b 2) acc)]
        [else (itmul (- a 1) b (+ acc b))]))

;;;;;;

(define (product term lo next hi)
  (define (iter lo result)
    (if (> lo hi)
        result
        (iter (next lo) (* (term lo) result))))
  (iter lo 1))

(define (next-pi a) (+ 2 a))
(define (term-pi a) (/ (* a a) (* (add1 a) (sub1 a))))
> (- (/ pi 2) (product term-pi 2 next-pi 32))  
0.023616965151431524

;;;;;;;;;

(define (repeated func times)
  (lambda (x)
    (if (> times 1) ((repeated func (sub1 times)) (func x))
        (func x))
    ))
Jump in the discussion.

No email address required.

https://i.rdrama.net/images/17025509830829637.webp

Jump in the discussion.

No email address required.

https://media.giphy.com/media/l2Je7gxQGyTpqAAuc/giphy.webp

Jump in the discussion.

No email address required.

Jump in the discussion.

No email address required.

Why arent you converting the SICP exercises to a different language

Jump in the discussion.

No email address required.

Do you mean the literal 40 bytes of code that was in prefix notation? Fine - I'll add some JS notation for the code-monkeys.

Jump in the discussion.

No email address required.

I remember one of you mentioned some.book and how it was great except it used some language that was disliked.

Jump in the discussion.

No email address required.

Literally every language has people who dislike it. I don't know if "some book" referred to SICP and "some language" referred to scheme, but if that's the case, then it already has been translated to JavaScript and Python, like I said in the OP.

Jump in the discussion.

No email address required.

I'm a mathlet, my original plan was reading Knuth's Concrete Mathematics and then "Coding the Matrix" for linear algebra before SICP but I'm not doing that shit :marseyretard2: I just want to coode :sneedcat2typing: Should I just jump right into SICP?

Jump in the discussion.

No email address required.

I'm a mathlet, my original plan was reading Knuth's Concrete Mathematics and then "Coding the Matrix" for linear algebra

Why?

I just want to coode :sneedcat2typing: Should I just jump right into SICP?

Sure - SICP is as good an intro book as any. It is a bit abstract and won't teach you a popular language, but it will definitely teach you how to go about writing code.

Jump in the discussion.

No email address required.

Why?

Idk its just what was recommended to me, concrete mathematics for a base and coding the matrix for linear algebra but I've been out of formal education too long and it's :marseysleeppat:

I already know how2coode I just want to better understand CS

Jump in the discussion.

No email address required.

Honestly just make projects that vaguely touch what you want to learn. Do you want to learn autismo optimization? Learn C/C++/Rust and learn about how memory allocation works, wanna make a basic telnet/SSH client as an intro to networking? Java/C#/Kotlin has a library for that. Do you want better stackoverflow answers? Find a moderately obscure language (like any Lisp) and watch all those :marseytunaktunak: wash away.

Literally just make projects, then find books and read relevant parts of them whenever you hit a wall. That's what I'd go for anyway, since the entire point of understanding CS is to write better programs, if you're not gonna write programs, idk what to say. I'll also add that most programming books tend to be a lot slower than necessary in my experience.

Jump in the discussion.

No email address required.

CS is a big field, so "understanding CS" is a hard thing to do. If you want to learn more about programming languages and software engineering, SICP is a good start. Otherwise, there are good books on Compilers, Operating Systems, Graphics Programming, Theory, etc.

Jump in the discussion.

No email address required.

Thanks for hosting this. I have now got around to reading the first chapter. If you decide to continue this series, it is likely that I will always need more time than a week.

I understand now why the book is so beloved. The first chapter starts of very strongly. The quote by John Locke particularly surprised me. I always thought that this is a very computer sciency way to look at things, but it appears that we just got inspired by a philosophy much older than computers.

The wizard analogy is also pure kino and I now finally understand the memes I'm spamming.

Not gonna reproduce the exercises here, I just did a few of them to practice the syntax. I definitely see the elegance of it, thoughbeit I don't know yet whether I find parens-soup aesthetically pleasing.

Jump in the discussion.

No email address required.

@pm-me-manifestos ping 3 days later because I think this got buried in your notifs. Just want to let your know that your thread was cool, even if I'm a bit tardy in reacting to it

Jump in the discussion.

No email address required.

What a ridiculous little culture.

Jump in the discussion.

No email address required.

Do introduction to computing by Yale patt. Let's do some assembly for the lc3


Read what I wrote above. Now picture in your head that I put a /s at the end. Good job sweaty! :marseygigaretard:

Jump in the discussion.

No email address required.

I haven't read that book: it seems similar to the Nand-to-tetris book, but less fun

Jump in the discussion.

No email address required.

It's extremely informative though. Probably learned more in that class and digital logic design than any other classes in school


Read what I wrote above. Now picture in your head that I put a /s at the end. Good job sweaty! :marseygigaretard:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.