Clojure 中的 Cond语句函数

首页 / Clojure入门教程 / Clojure 中的 Cond语句函数

Clojure提供了另一个判断语句,称为" cond" 语句。

Cond - 语法

以下是此声明的一般形式。

cond
(expression evaluation1) statement #1
(expression evaluation2) statement #2
(expression evaluationN) statement #N
:else statement #Default

该语句的一般工作如下-

Cond - 示例

以下是Clojure中" cond"语句的示例。

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

;; This program displays Hello Learnfk
(defn Example []
   (def x 5)
   (cond
      (= x 5) (println "x is 5")
      (= x 10)(println "x is 10")
      :else (println "x is not defined")))
(Example)

在上面的示例中,我们首先将变量x初始化为值5。然后,我们有一个" cond"语句来判断变量" x"的值。基于变量的值,它将执行相关的语句集。

上面的代码产生以下输出。

x is 5

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

重学前端 -〔程劭非(winter)〕

iOS开发高手课 -〔戴铭〕

视觉笔记入门课 -〔高伟〕

编译原理实战课 -〔宫文学〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

Python自动化办公实战课 -〔尹会生〕

程序员的个人财富课 -〔王喆〕

React Native 新架构实战课 -〔蒋宏伟〕

AI绘画核心技术与实战 -〔南柯〕

好记忆不如烂笔头。留下您的足迹吧 :)