Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Modulus in LISP

Tags:

lisp

modulus

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?

like image 612
Nick Welki Avatar asked Apr 18 '11 17:04

Nick Welki


People also ask

What is mod in Lisp?

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.

How do you get a remainder on a lisp?

In Lisp, the function for computing a remainder is % . The function returns the remainder of its first argument divided by its second argument.

How do you write a mod in scheme?

Scheme modulo is implemented as ((n%m)+m)%m.


2 Answers

How about mod, from the page:

(mod -1 5) => 4                                                              
(mod 13 4) => 1                                                              
(mod -13 4) => 3                                                             
(mod 13 -4) => -3                
like image 64
Trey Jackson Avatar answered Sep 26 '22 03:09

Trey Jackson


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.

like image 35
Terje Norderhaug Avatar answered Sep 25 '22 03:09

Terje Norderhaug