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

(defn name
"I am the doc string to describe the function"
  (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

(defn name
  "I am the doc string to describe the function"
    (str "behaviour with no args"))
    (str "behaviour with one arg"))
  ([one two & args]
    (str "behaviour with multiple args")))

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

Reveal answer

(defn i-am-polly
  ([] (i-am-polly "My name is polly"))
  ([message] (str message)))

(i-am-polly "I call different behaviour depeding on arguments sent")

results matching ""

    No results matching ""