On this page:
1.1 Func Defs
1.2 Func Defs
1.3 Func Defs
1.4 Func Defs
1.5 Func Defs
1.6 Func Defs
1.7 Func Defs
1.8 Func Defs
1.9 Func Defs
1.10 Func Defs
1.11 Func Defs
1.12 Func Defs
1.13 Func Defs
1.14 Func Defs
1.15 Func Defs
1.16 Func Defs

1 Coding Fundamentals

1.1 Func Defs

read
Define a function named foo that returns the number 42. Call it after you define it.
code
#lang fundamentals
 
(define (foo) 42)
 
(foo)

1.2 Func Defs

read
Define a function named double that takes a number and multiplies it by 2. Call double on the number 756. Call double twice on the number 422.
code
#lang fundamentals
 
(define (double x) (* 2 x))
 
(double 756)
 
(double (double 422))

1.3 Func Defs

read
Define a function named end-of-string that takes a string and returns everything except the first character. Call the function on your full name.
code
#lang fundamentals
 
(define (end-of-string x) (substring x 1))
 
(end-of-string "Albert Einstein")

1.4 Func Defs

read
Define a function named small-circle that takes a symbol and returns a small circle of that color. Call that function to produce an orange circle.
code
#lang fundamentals
 
(define (small-circle color)
   (circle 40 'solid color))
 
(small-circle 'orange)

1.5 Func Defs

read
Define a function named random-circle that takes a symbol and returns a randomly sized circle of that color. Call that function five times to produce five different colored circle of various sizes.
code
#lang fundamentals
 
(define (random-circle color)
   (circle (add1 (random 100)) 'solid color))
 
(random-circle 'red)
 
(random-circle 'orange)
 
(random-circle 'yellow)
 
(random-circle 'green)
 
(random-circle 'blue)

1.6 Func Defs

read
Define a function named add-1-and-square that takes a number, adds 1 to it, and squares it. Call it on two positive numbers and two negative numbers of your choice.
code
#lang fundamentals
 
(define (add-1-and-square x) (sqr (add1 x)))
 
(add-1-and-square -1)
 
(add-1-and-square -5)
 
(add-1-and-square 22)
 
(add-1-and-square 41)

1.7 Func Defs

read
Define a function called is-cool that takes two strings and puts them together with a space in between and the string "is cool" at the end. Call it on your own name and the names of two other people’s names.
code
#lang fundamentals
 
(define (is-cool a b)
   (string-append a " " b " is cool"))
 
(is-cool "Stephen" "Foster")
 
(is-cool "Ada" "Lovelace")
 
(is-cool "Alan" "Turing")

1.8 Func Defs

read
Define a function called repeat-3 that takes one string and returns that string repeated 3 times with spaces in between. Call it on a string of your choice. Call it twice on a different string to make it repeat 9 times.
code
#lang fundamentals
 
(define (repeat-3 s)
   (string-append s " " s " " s))
 
(repeat-3 "Stephen")
 
(repeat-3 (repeat-3 "¡Hola!"))

1.9 Func Defs

read
Define a function called yes-no that takes a boolean and returns the string "yes" when the boolean is true and "no" when it is false. Call it on both true and false.
code
#lang fundamentals
 
(define (yes-no b) (if b "yes" "no"))
 
(yes-no #t)
 
(yes-no #f)

1.10 Func Defs

read
Define a function called red-green which takes a boolean and returns a green circle or a red circle depending on if the boolean is true or false. Call it on both true and false.
code
#lang fundamentals
 
(define (red-green b)
   (if b
     (circle 40 'solid 'green)
     (circle 40 'solid 'red)))
 
(red-green #t)
 
(red-green #f)

1.11 Func Defs

read
Define a function called circle-star which takes a boolean and returns either a circle or a star depending on the value of the boolean. Call it on both true and false.
code
#lang fundamentals
 
(define (circle-star b)
   (if b
     (circle 40 'solid 'green)
     (star 40 'solid 'green)))
 
(circle-star #t)
 
(circle-star #f)

1.12 Func Defs

read
Define a function called colored-circle-triangle that takes a boolean and a string. The boolean should control whether the function returns a circle or a triangle, the color string should determine the color of the shape. Call several times to produce 3 circles and two triangles, all of different colors.
code
#lang fundamentals
 
(define (colored-circle-triangle b color)
   (if b
     (circle 40 'solid color)
     (triangle 40 'solid color)))
 
(colored-circle-triangle #t "red")
 
(colored-circle-triangle #t "orange")
 
(colored-circle-triangle #t "yellow")
 
(colored-circle-triangle #f "green")
 
(colored-circle-triangle #f "blue")
 
(colored-circle-triangle #f "purple")

1.13 Func Defs

read
Define a function called sum-or-diff that takes two numbers and a boolean. If the boolean is true, the function should return the sum of the two numbers. Otherwise, it should return the difference. Call it 5 times on a variety of inputs.
code
#lang fundamentals
 
(define (sum-or-diff b x y)
   (if b (+ x y) (- x y)))
 
(sum-or-diff #t 2 2)
 
(sum-or-diff #f 2 2)
 
(sum-or-diff #t 100 300)
 
(sum-or-diff #f 100 300)
 
(sum-or-diff #f 71 -11)

1.14 Func Defs

read
Define a function called max-or-min that takes two numbers and a boolean. If the boolean is true, the function should return the greater of the two numbers. Otherwise, it should return the lesser. Call it 5 times on a variety of inputs.
code
#lang fundamentals
 
(define (max-or-min b x y)
   (if b (max x y) (min x y)))
 
(max-or-min #t 2 2)
 
(max-or-min #f 2 2)
 
(max-or-min #t 100 300)
 
(max-or-min #f 100 300)
 
(max-or-min #f 71 -11)

1.15 Func Defs

read
Define a function called magic-14 that takes a number. If the number is 14, it should return a blue circle. For any other number, it should return a red square. Call it twice to produce both a blue circle and a red square.
code
#lang fundamentals
 
(define (magic-14 n)
   (if (= 14 n)
     (circle 40 'solid 'blue)
     (square 40 'solid 'red)))
 
(magic-14 10)
 
(magic-14 14)

1.16 Func Defs

read
Define a function called magic-22/45 that takes a number. If the number is 22 or 45, it should return a purple star. For any other number, it should return a red square. Call it twice to produce both a purple star and a red square.
code
#lang fundamentals
 
(define (magic-22/45 n)
   (if (or (= 22 n) (= 45 n))
     (star 40 'solid 'purple)
     (square 40 'solid 'red)))
 
(magic-22/45 22)
 
(magic-22/45 10)