examples/htdp/chapter2-2.rkt
#lang planet wrturtle/pyret/bsl

# HtDP Chapter 2, Section 2

# Exercise 13
fun dist_from_origin(x,y): sqrt(x*x + y*y)

test: dist_from_origin(3,4) is: 5
test: dist_from_origin(5,12) is: 13

# Exercise 14
fun cube_volume(x): x * x * x

test: cube_volume(1) is: 1
test: cube_volume(2) is: 8
test: cube_volume(3) is: 27

fun cube_surface(x): 6 * x * x

test: cube_surface(1) is: 6
test: cube_surface(2) is: 24

# Exercise 15
fun string_first(str): str[0]

test: string_first('cat') is: 'c'
test: string_first('dog') is: 'd'

# Exercise 16
fun string_last(str): str[len(str)-1]

test: string_last('cat') is: 't'
test: string_last('dog') is: 'g'

# Exercise 17
fun bool_imply(b1,b2): b1 = False or b2 = True

test: bool_imply(False, False) is: True
test: bool_imply(False, True) is: True
test: bool_imply(True, False) is: False
test: bool_imply(True, True) is: True

# Exercise 18
fun string_join(s1, s2): s1 + "_" + s2

test: string_join("hello", "world") is: "hello_world"

# Exercise 19
fun string_insert(s, i): s[0:i] + "_" + s[i:len(s)]

test: string_insert("helloworld", 5) is: "hello_world"

# Exercise 20
fun string_delete(s, i): s[0:i] + s[i+1:len(s)]

test: string_delete("helloworld", 5) is: "helloorld"

# Movie theaters and profits
fun attendees(ticket_price): 120 + ((15 / 0.1) * (5.0 - ticket_price))
fun revenue(ticket_price): attendees(ticket_price) * ticket_price
fun cost(ticket_price): 180 + (0.04 * attendees(ticket_price))
fun profit(ticket_price): revenue(ticket_price) - cost(ticket_price)

# Exercise 23
# > profit(1)
# 511.2
# > profit(2)
# 937.2
# > profit(3)
# 1063.2
# > profit(4)
# 889.2
# > profit(5)
# 415.2
#
# The answer is thus, $3

# Exercise 24
fun new_cost(ticket_price): 1.5 * attendees(ticket_price)
fun new_profit(ticket_price): revenue(ticket_price) - new_cost(ticket_price)

# > new_profit(3)
# 630.0
# > new_profit(4)
# 675.0
# > new_profit(5)
# 420.0
#
# So now, the answer is $4

# batch-io
write_file("sample.dat", "212")
test: read_file("sample.dat") is: "212"