Ploymorphism

Usually you define a function with one set of arguments, either none [], one [one] or many [any number of args], using the basic syntax

  1. (defn name
  2. "I am the doc string to describe the function"
  3. [args]
  4. (str "define your behaviour here"))

Instead of writing multiple functions with the same name that each take different numbers of arguments, you can use the following polymorphic syntax in Clojure

  1. (defn name
  2. "I am the doc string to describe the function"
  3. ([]
  4. (str "behaviour with no args"))
  5. ([one]
  6. (str "behaviour with one arg"))
  7. ([one two & args]
  8. (str "behaviour with multiple args")))

Note Write a simple function called i-am-polly that returns a default message when given no arguments and a custom message when given a custom string as an argument

  1. (defn i-am-polly
  2. ([] (i-am-polly "My name is polly"))
  3. ([message] (str message)))
  4. (i-am-polly)
  5. (i-am-polly "I call different behaviour depeding on arguments sent")