开发者

How to get the Modulus in LISP

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?


How about mod, from the page:

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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜