Clojure 定義函數(shù)

2018-12-20 16:17 更新

函數(shù)通過使用'defn'宏來定義。 以下是函數(shù)定義的一般語法。

語法

(defn functionname
   “optional documentation string”
   [arguments]
   (code block))

函數(shù)可以有文檔字符串,這很好描述函數(shù)實(shí)際上做什么。

下面是一個(gè)簡(jiǎn)單的函數(shù)示例。

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

;; This program displays Hello World
(defn Example []
   (def x 1)
   (def y 1.25)
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)

在上面的例子中,函數(shù)的名稱是Example。

以上示例將輸出以下結(jié)果:

1
1.25
Hello


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)