Clojure在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
抽取序列中某几个字符中第一次出现的位置 发布于:2022-09-03 15:43 hello world 发布于:2021-12-04 16:38 [更多]
显示目录

运算符



搭载国产梦丘操作系统,支持安装软件,在线编程可视化开发软件,无需配置开发环境

点击购买 梦丘固件

Clojure 运算符

运算符是一个符号,通知编译器执行特定的数学或逻辑操作。

Clojure有以下类型的运算符:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 位运算符

注 -在Clojure中,运算符和操作数以下面的语法方式工作。

语法

(operator operand1 operand2 operandn)

例

(+ 1 2)

上述示例对数字1和2进行算术运算。

算术运算符 Clojure语言支持任何语言的正常的算术运算符。 以下是Clojure中提供的算术运算符。

操作 描述 例
+ 两个操作数相加 (+ 1 2) 得到 3
- 从第一个操作数中减去第二个操作数 (- 2 1) 得到 1
* 两个操作数的乘法 (* 2 2) 得到 4
/ 分子除以分母 (float (/ 3 2)) 得到 1.5
inc 用于将操作数的值增加1的增量运算符 inc 5 得到 6
dec 用于将操作数的值减1的增量运算符 dec 5 得到 4
max 返回其最大值的参数 max 1 2 3 返回 3
min 返回其最小值的参数 min 1 2 3 返回 1
rem 将第一个数除以第二个数的余数 rem 3 2 得到 1

例

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (+ 2 2))
   (println x)

   (def x (- 2 1))
   (println x)

   (def x (* 2 2))
   (println x)

   (def x (float(/ 2 1)))
   (println x)

   (def x (inc 2))
   (println x)

   (def x (dec 2))
   (println x)

   (def x (max 1 2 3))
   (println x)

   (def x (min 1 2 3))
   (println x)

   (def x (rem 3 2))
   (println x))
(Example)

上述示例产生以下结果:

4
1
4
2.0
3
1
3
1
1

关系运算符

关系运算符允许比较对象。 以下是Clojure中可用的关系运算符。

操作者 描述 例
= 测试两个对象之间的相等性 (= 2 2) 得到 true
not= 测试两个对象之间的差异 (not = 3 2) 得到 true
< 检查左对象是否小于右对象正确的运算符 (< 2 3) 得到 true
<= 检查左对象是否小于或等于右对象的运算符 (<= 2 3) 得到 true
> 检查左对象是否大于右对象的运算符 (> 3 2) 得到 true
> = 检查左对象是否大于或等于右对象的运算符 (>= 3 2) 得到 true

例

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (= 2 2))
   (println x)

   (def x (not= 3 2))
   (println x)

   (def x (< 2 3))
   (println x)

   (def x (<= 2 3))
   (println x)

   (def x (> 3 2))
   (println x)

   (def x (>= 3 2))
   (println x))
(Example)

上述示例产生以下结果:

true
true
true
true
true
true

逻辑运算符

逻辑运算符用于计算布尔表达式。 以下是Groovy中可用的逻辑运算符。

操作者 描述 例
or 这是逻辑“或”运算 (or true true)会得到 true
and 这是逻辑“与”运算 (and true false)会得到 false
not 这是逻辑“非”运算 (not false)会得到 true

以下代码段显示了如何使用各种运算符。

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (or true true))
   (println x)

   (def x (and true false))
   (println x)

   (def x (not true))
   (println x))
(Example)

上述示例产生以下结果:

true
false
false

位运算符

Groovy提供了四个按位运算符。 以下是Groovy中可用的按位运算符。

这是位取反运算符

运算符 运算符说明
bit-and 这是位“和”运算符
bit-or 这是位“或”运算符
bit-xor 这是位“xor”或Exclusive'or'运算符
bit-not 这是位取反运算符

以下是这些运算符的真值表。

p q p&Q p 丨 q p ^ Q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

以下代码段显示了如何使用各种运算符:

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def x (bit-and 00111100 00001101))
   (println x)

   (def x (bit-or 00111100 00001101))
   (println x)

   (def x (bit-xor 00111100 00001101))
   (println x)) 
(Example)

上述示例产生以下结果:

576
37441
36865

运算符优先级

与LISP一般情况一样,没有必要担心运算符优先级。 这是S表达式和前缀符号的好处之一。 所有函数从左到右和从内到外。 Clojure中的运算符只是函数,并且一切都完全括起来。

由JSRUN为你提供的Clojure在线运行、在线编译工具
        JSRUN提供的Clojure 在线运行,Clojure 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。