18

I am learning LISP right now and I haven't found anything on how to get the modulus in LISP. Is there someway to get it inside of a function? I know other languages like Java use % in order to find the modulus, but what does LISP use?

Nick Welki
  • 435
  • 4
  • 8
  • 13

4 Answers4

27

How about mod, from the page:

(mod -1 5) => 4                                                              
(mod 13 4) => 1                                                              
(mod -13 4) => 3                                                             
(mod 13 -4) => -3                
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
10

As an alternative to mod, the Common Lisp floor function returns modulo as its second value. This is useful in cases where you are also interested in the quotient.

Terje Norderhaug
  • 3,649
  • 22
  • 25
4

There are two options:

mod and rem are generalizations of the modulus and remainder functions respectively.

mod performs the operation floor on number and divisor and returns the remainder of the floor operation.

rem performs the operation truncate on number and divisor and returns the remainder of the truncate operation.

mod and rem are the modulus and remainder functions when number and divisor are integers.

Examples:

>  (rem -1 5) =>  -1  
>  (mod -1 5) =>  4  
>  (mod 13 4) =>  1  
>  (rem 13 4) =>  1

Source: http://clhs.lisp.se/Body/f_mod_r.htm

Touchstone
  • 5,575
  • 7
  • 41
  • 48
0

In Lisp, the command for Modulus function is rem -reminder Example (rem 13 4) result 1