What is the zero method in Clojure numbers?

Overview

We use the zero method to test if a number is zero.

Syntax

(zero? number)
Syntax for zero method

Parameter

The zero method accepts just one parameter, the number itself, as illustrated in the syntax section.

Return value

The zero method returns true if the number is 0 and false if the number is greater or less than 0.

Application

We use the zero method to ensure that the calculations we are making does not include a zero. Thus, we use the zero method to test the number. Let's look at the example below:

Example

(ns clojure.examples.hello
(:gen-class))
;; This program displays Hello World
(defn zerro []
(def x (zero? 0))
(println x)
(def x (zero? -1))
(println x)
(def x (zero? 9))
(println x))
(zerro)

Explanation

From the code above:

  • Line 5: We define a function zerro.
  • Line 6: We pass in 0 into the zero method.
  • Line 7: We print the output, notice that the output we get is true because 0 is 0 .
  • Line 9: We pass in -1 into the zero method.
  • Line 10: We print the output, notice that the output we get is false because -1 is not 0 .
  • Line 12: We pass in 9 into the zero method.
  • Line 13: We print the output, notice that the output we get is false because 9 is an odd number and not 0.
  • Line 14: We call our zerro function to execute the code.

Free Resources