Module 3

Functions

A function

What are functions?

  • count, conj, first
  • +, -, *, /
  • A piece of code that takes values and returns a value

An example function

(defn total-bill
  "Given subtotal of bill, return total after tax."
  [subtotal]
  (* 1.08 subtotal)) 

(total-bill 8.5)  ;=> 9.18
(total-bill 50)   ;=> 54.0
(total-bill 50/6) ;=> 9.0

An example function

(defn ; specifies that we are defining a function
  total-bill ; the name of this function
  
  ;; documentation, optional
  "Given subtotal of bill, return total..."
  
  [subtotal] ; list of arguments

  ;; body of function
  (* 1.08 subtotal))

A function with multiple arguments.

(defn total-with-tip
  "Given subtotal, return total after tax and tip."
  [subtotal tip-pct] ;; takes 2 arguments
  (* 1.08 subtotal (+ 1 tip-pct)))

(total-with-tip 12 0.18) ;=> 15.93

Exercise: Find per-person share of bill among a group

Create a new function, share-per-person, that takes three arguments: the subtotal, the tip percent, and the number of people in the group.

It should call our total-with-tip function but change the result to return the average amount each person should pay.

Naming functions

  • Functions are named like any other value
  • Predicate functions usually end in ?

    • zero?
    • vector?
    • empty?

Functions that take other functions

map

(map plus-one [1 2 3 4 5])
  ;=> [2 3 4 5 6]

map

map

(def dine-in-orders [12.50 20 21 16 18.40])
(def take-out-orders [6.00 6.00 7.95 6.25])

(map total-bill dine-in-orders)
  ;=> [13.5 21.6 22.68 17.28 19.872]
(map total-bill take-out-orders)
  ;=> [6.48 6.48 8.586 6.75]

reduce

(reduce + [1 2 3 4 5]) ;=> 15
(reduce max [8 17 2]) ;=> 17

reduce

reduce

reduce in action

(def take-out-totals [6.48 6.48 8.586 6.75])

(reduce + take-out-totals) ;=> 28.296

reduce in action

(def take-out-totals [6.48 6.48 8.586 6.75])
(reduce + take-out-totals) ;=> 28.296

(+ 6.48   6.48)  ;=> 12.96
(+ 12.96  8.586) ;=> 21.546
(+ 21.546 6.75)  ;=> 28.296

Exercise: Find the average

Create a function called average that takes a vector of bill amounts and returns the average of those amounts.

Hint: you will need to use the functions reduce and count.