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
0into thezeromethod.
- Line 7: We print the output, notice that the output we get is
truebecause0is0.
- Line 9: We pass in
-1into thezeromethod.
- Line 10: We print the output, notice that the output we get is
falsebecause-1is not0.
- Line 12: We pass in
9into thezeromethod.
- Line 13: We print the output, notice that the output we get is
falsebecause9is an odd number and not0.
- Line 14: We call our
zerrofunction to execute the code.