Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Clojure Programming/Examples/API Examples/Data Structures

From Wikibooks, open books for an open world
<Clojure Programming |Examples |API Examples

Numbers

[edit |edit source]

Computation

[edit |edit source]
+
[edit |edit source]

Returns the sum of nums. (+) returns 0.

(+ [] [x] [x y] [x y & more])

user=> (+ 1 2 3)6user=> (map + [1 2 3] [1 1 2])(2 3 5)
-
[edit |edit source]

If no ys are supplied, returns the negation of x, else subtracts the ys from x and returns the result.

(- [x] [x y] [x y & more])

user=> (- 1 2 3)-4user=> (- -1)1
*
[edit |edit source]

Returns the product of nums. (*) returns 1.

(* [] [x] [x y] [x y & more])

user=> (* 1 2 3 4)24
/
[edit |edit source]

If no denominators are supplied, returns 1/numerator, else returns numerator divided by all of the denominators.

(/ [x] [x y] [x y & more])

user=> (/ 1 4)1/4

inc

[edit |edit source]

Returns a number one greater than num.

(inc [x])

user=> (inc 41) 42user=> (inc 1/4)5/4

dec

[edit |edit source]

Returns a number one less than num.

(dec [x])

user=> (dec 99)98user=> (map dec [2 3 4])(1 2 3)

min

[edit |edit source]

Returns the least of the nums.

(min [x] [x y] [x y & more])

user=> (min 2 -1 3)-1

max

[edit |edit source]

Returns the greatest of the nums.

(max [x] [x y] [x y & more])

user=> (max 2 -1 3)3

quot

[edit |edit source]

Quotient of dividing numerator by denominator.

(quot [num div])

user=> (quot 4 2)2user=> (quot 5 2)2user=> (quot 6 2)3

rem

[edit |edit source]

Remainder of dividing numerator by denominator.

(rem [num div])

user=> (rem 5 2) 1user=> (rem 4 2)0

rationalize

[edit |edit source]

Returns the rational value of num.

(rationalize [num])

user=> (rationalize 1.33)      133/100user=> (rationalize 3.14159265)62831853/20000000user=> (rationalize (Math/sqrt 2))        14142135623730951/10000000000000000

Comparison

[edit |edit source]

==

[edit |edit source]

Returns non-nil if nums all have the same value, otherwise false.

(== [x] [x y] [x y & more])

user=> (== (+ 1 2) 3)trueuser=> (== (+ 1 2) 3 2.999)falseuser=> (if (== 2 (+ 1 1)) (println "So easy!"))So easy!user=> (== true true) ;Be careful! Works only for nums. falseuser=> (= true true) ;= works like Java x.equals(y)true

<

[edit |edit source]

Returns non-nil if nums are in monotonically increasing order, otherwise false.

(< [x] [x y] [x y & more])

user=> (< 1 (Math/sqrt 2) (+ 1 1))trueuser=> (< 5 5)                      falseuser=> (< 5)true

>

[edit |edit source]

Returns non-nil if nums are in monotonically decreasing order, otherwise false.

(> [x] [x y] [x y & more])

user=> (> 7 6 4 2)trueuser=> (> 2 4)    false

<=

[edit |edit source]

Returns non-nil if nums are in monotonically non-decreasing order, otherwise false.

(<= [x] [x y] [x y & more])

user=> (<= 0 1 1)true

>=

[edit |edit source]

Returns non-nil if nums are in monotonically non-increasing order, otherwise false.

(>= [x] [x y] [x y & more])

user=> (>= 3 2 1)true

Predicates

[edit |edit source]

zero?

[edit |edit source]

Returns true if num is zero, else false.

(zero? [x])

user=> (zero? 0.01) falseuser=> (zero? 0)   true

pos?

[edit |edit source]

Returns true if num is greater than zero, else false.

(pos? [x])

user=> (pos? 0)        falseuser=> (pos? (Math/PI))trueuser=> (filter pos? [-1 1 0 -1 5 2])(1 5 2)

neg?

[edit |edit source]

Returns true if num is less than zero, else false.

(neg? [x])

user=> (neg? -1)       trueuser=> (neg? 0) false

Strings

[edit |edit source]

str

[edit |edit source]
user=> (str "a" :b \c 3 4.5 6/7)"a:bc34.56/7"user=> (str [1 2])"[1 2]"user=> (str 1 2)"12"user=> (str #(1))"user$eval__1289$fn__1291@4f53dd"

Characters

[edit |edit source]
user=> \a\auser=> (seq "a")\auser=> (seq " ")(\space)user=> (char 13)\returnuser=> (str \a \b \c)"abc"user=> (char? \z)true
Retrieved from "https://en.wikibooks.org/w/index.php?title=Clojure_Programming/Examples/API_Examples/Data_Structures&oldid=3230475"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp