(define x 5)
(define y 12)
(sqrt (+ (* x x) (* y y)))
(define prefix "hello")
(define suffix "world")
(string-append prefix "_" suffix)
(define str "helloworld")
(define i 5)
(string-append (substring str 0 i)
"_"
(substring str i))
(define str "helloworld")
(define ind "0123456789")
(define i 5)
(string-append (substring str 0 i)
(substring str (+ i 1)))
[0, 9]
#lang htdp/bsl
(require 2htdp/image)
(define COLOR "aquamarine")
(define HEIGHT 60)
(define WIDTH 300)
(define h HEIGHT)
(define w (/ WIDTH 3))
(beside
(triangle/sas h 90 w "solid" COLOR)
(rectangle w h "solid" COLOR)
(triangle/ass 90 h w "solid" COLOR))
(require 2htdp/image)
(define cat (bitmap/url "https://htdp.org/2019-02-24/cat1.png"))
(* (image-width cat) (image-height cat))
(define sunny #true)
(define friday #false)
(or (not sunny) friday)
4
(require 2htdp/image)
(define cat (bitmap/url "https://htdp.org/2019-02-24/cat1.png"))
(if (>= (image-height cat) (image-width cat))
"tall"
"wide")
(require 2htdp/image)
;; 宽 高
(define pic (rectangle 100 50 "solid" "orange"))
(if (>= (image-height pic) (image-width pic))
"tall"
"wide")
(require 2htdp/image)
;; (define image (circle 100 "solid" "green"))
;; (define image (rectangle 100 50 "solid" "green"))
(define image (rectangle 50 100 "solid" "green"))
(define width (image-width image))
(define height (image-height image))
(if (> width height)
"wide"
(if (< width height)
"tall"
"square"))
(require 2htdp/image)
;; (define in "hello")
;; (define in (circle 5 "solid" "red"))
;; (define in 0)
(define in #false)
(if (string? in)
(string-length in)
(if (image? in)
(* (image-width in) (image-height in))
(if (number? in)
(if (<= in 0)
in
(- in 1))
(if in
10
20))))
(define (distance x y)
(sqrt (+ (* x x) (* y y))))
(distance 3 4)
(distance 5 12)
(distance 5 0)
(distance 1 1)
(define (cvolume a)
(* a a a))
(define (csurface a)
(* 6 (* a a)))
(cvolume 1)
(csurface 1)
(define (string-first s) (substring s 0 1))
(string-first "hello")
(define (string-last s)
(substring
s
(- (string-length s) 1)
(string-length s)))
(string-last "hello")
(define (==> sunny friday)
(or (not sunny) friday))
(==> #false #true)
(require 2htdp/image)
(define (image-area image)
(* (image-width image) (image-height image)))
(image-area (rectangle 10 20 "solid" "pink"))
(require 2htdp/image)
(define (image-classify image)
(if (> (image-width image) (image-height image))
"wide"
(if (< (image-width image) (image-height image))
"tall"
"square")))
(image-classify (circle 10 "solid" "blue"))
(image-classify (rectangle 10 20 "solid" "red"))
(image-classify (rectangle 20 10 "solid" "green"))
(define (string-join s1 s2)
(string-append s1 "_" s2))
(string-join "hello" "world")
(define (string-insert str i)
(string-append (substring str 0 i)
"_"
(substring str i (string-length str))))
(string-insert "helloworld" 5)
(string-insert "" 0)
(define (string-delete str i)
(string-append (substring str 0 i)
(substring str (+ i 1))))
(string-delete "hello" 4)
;; can't deal with empty string
;; (string-delete "" 0)
(+ (ff 1) (ff 1))
中,DrRacket 的 stepper 没有重用 (ff 1)
的值。
(define base-attendees 120)
(define base-price 5.0)
(define attendees-change-per-cent 15)
(define (attendees ticket-price)
(- base-attendees
(* (/ (- ticket-price base-price) 0.1)
attendees-change-per-cent)))
(define (revenue ticket-price)
(* ticket-price (attendees ticket-price)))
(define fix-cost 0)
(define cost-per-attendee 1.5)
(define (cost ticket-price)
(+ fix-cost (* cost-per-attendee (attendees ticket-price))))
(define (profit ticket-price)
(- (revenue ticket-price)
(cost ticket-price)))
(profit 1)
(profit 2)
(profit 3)
(profit 4)
(profit 5)
(define price-sensitivity (/ 15 0.1))
声音、摄像头、键盘、鼠标、触摸板、温度、亮度、手势、触摸屏、蓝牙、wifi
;; String -> String
;; extracts the first character from a non-empty string
;; given: "hello", expect: "h"
(define (string-first s)
(substring s 0 1))
(string-first "hello")
(string-first "world")
;; String -> String
;; extracts the last character from a non-empty string
;; given: "hello", expect "o"
(define (string-last s)
(substring s (sub1 (string-length s))))
(string-last "hello")
(string-last "world")
;; Image -> Number
;; Counts the number of pixels of img
;; given: (square 10 "solid" "red"), expect 100
;; given: (rectangle 5 10 "solid" "red"), expect 50
(define (image-area img)
(* (image-width img) (image-height img)))
(require 2htdp/image)
(image-area (square 10 "solid" "red"))
(image-area (rectangle 5 10 "solid" "red"))
;; String -> String
;; return a string like s with the first char removed
;; given: "hello", expect "ello"
(define (string-rest s)
(substring s 1))
(string-rest "hello")
(string-rest "world")
;; String -> String
;; return a string like s with the last char removed
;; given: "hello", expect "hell"
(define (string-remove-last s)
(substring s 0 (sub1 (string-length s))))
(string-remove-last "hello")
(string-remove-last "world")
(check-duplicates)
时间 t,速度 sin(t),距离是多少?
x*sin(x) 的积分?
4