Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Last active January 19, 2021 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/4515fa52209db829a4e936627ebcfdcb to your computer and use it in GitHub Desktop.
Save sojohnnysaid/4515fa52209db829a4e936627ebcfdcb to your computer and use it in GitHub Desktop.
How to code simple data 1a: Beginning Student Language - my problem set solutions
;;---------------------------
;; arithmetic expression
;;---------------------------
(* 3 5 7)
(* (* 3 5) 7)
;;---------------------------
;; tile-starter
;;---------------------------
(above (beside(square 20 "solid" "blue")
(square 20 "solid" "yellow"))
(beside(square 20 "solid" "yellow")
(square 20 "solid" "blue")))
;;---------------------------
;; compare-images-starter.rkt
;;---------------------------
(define IMAGE1 (rectangle 10 15 "solid" "red"))
(define IMAGE2 (rectangle 15 10 "solid" "red"))
(if (> (image-height IMAGE1)
(image-height IMAGE2))
"IMAGE1 is taller than IMAGE2"
"IMAGE1 is not taller than IMAGE2")
(if (< (image-width IMAGE1)
(image-width IMAGE2))
"IMAGE1 is narrower than IMAGE2"
"IMAGE1 is not narrower than IMAGE2")
(if
(and (=(image-width IMAGE1)
(image-width IMAGE2))
(=(image-height IMAGE1)
(image-height IMAGE2)))
"IMAGE1 has both the same width AND height as IMAGE2"
"IMAGE1 does not have the same width AND height as IMAGE2")
;;---------------------------
;; more-foo-evaluation
;;---------------------------
(define (foo n)
(* n n))
;define function called but nothing to evaluate
(foo (+ 3 4))
;function arguments (n) replaced by 7
(foo 7)
(* 7 7)
49
;;---------------------------
;; function-writing-starter
;;---------------------------
(define (returnsTheLargerNumber x y) (if(> x y)x y))
(returnsTheLargerNumber 25 10)
;;---------------------------
;; foo-evaluation
;;---------------------------
(define (foo s)
(if (string=? (substring s 0 1) "a")
(string-append s "a")
s))
(foo (substring "abcde" 0 3))
(foo "abc")
;"abc" replaces every instance of the s arg in the function definition
(if (string=? "a" "a")
(string-append "abc" "a")
"abc")
(if #true (string-append "abc" "a") "abc")
(string-append "abc" "a")
"abca"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment