clojure-基本语法-函数定义

来源:互联网 发布:mysql命令大全查询 编辑:程序博客网 时间:2024/05/19 11:48
一、创建函数: 
fn: 
fn是一个宏(后面进行详细描述),用于定义一个简单的函数,如下: 
Java代码  收藏代码
  1. user=> (fn [] "hello")  
  2. #<user$eval375$fn__376 user$eval375$fn__376@eabd2f>  
  3. user=> ((fn [] "hello"))  
  4. "hello"  
  5. user=> ((fn [x] x) "hello") ; 带参数  
  6. "hello"  

简短的函数可以使用#(),%表示唯一的参数;%1、%2 ..表示第1、2、..个参数;%&表示所有参数,如下: 
Java代码  收藏代码
  1. user=> (#(/ % 34);结果为3/4  
  2. 4/3  
  3. user=> (#(/ %2 %13 4);结果为3/4  
  4. 4/3  
  5. user=> (#(apply / %&) 3 5 7);结果为3/5/7  
  6. 3/35  

下面是几个定义函数的例子: 
Java代码  收藏代码
  1. user=> ((fn [x] (+ 1 x)) 3);一个参数,完成加一的功能  
  2. 4  
  3. user=> (#(+ 1 %) 3);使用#符号完成加一的功能  
  4. 4  
  5. user=> ((fn [x y] (* x y)) 3 4);两个参数,实现乘积的功能  
  6. 12  
  7. user=> (#(* %1 %23 4);使用#符号完成两个参数乘积的功能  
  8. 12  


defn: 
defn 宏用来定义一个函数。它的参数包括一个函数名字,一个可选的注释字符串,参数列表,然后一个方法体。而函数的返回值则是方法体里面最后一个表达式的值。所有的函数都会返回一个值, 只是有的返回的值是nil。 
Java代码  收藏代码
  1. user=> (defn f1 [] "hello");定义无参函数  
  2. #'user/f1  
  3. user=> (f1)  
  4. "hello"  
  5. user=> (defn f2 [x] (format "hello %s" x));定义一个参数函数  
  6. #'user/f2  
  7. user=> (f2 "girl")  
  8. "hello girl"  
  9. user=> (defn f3 [x y] (+ x y));定义两个参数相加的函数  
  10. #'user/f3  
  11. user=> (f3 2 4)  
  12. 6  
  13. user=> (defn f4 "f4 function" [] (println "f4 function"));带注释的函数  
  14. #'user/f4  
  15. user=> (f4)  
  16. f4 function  
  17. nil  
  18. user=> (doc f4);通过doc查看函数注释信息  
  19. -------------------------  
  20. user/f4  
  21. ([])  
  22.   f4 function  
  23. nil  
  24. user=> (defn f5 ([] (str "no parameter"))  
  25.   #_=> ([name] (str "my name is " name)));定义重载的函数  
  26. #'user/f5  
  27. user=> (f5)  
  28. "no parameter"  
  29. user=> (f5 "clojure")  
  30. "my name is clojure"  
  31. user=> (defn f1 [& a] (str a));定义变参函数  
  32. #'user/f1  
  33. user=> (f1 1 2 3)  
  34. "(1 2 3)"  
  35. user=> (defn m [& arg] (str arg ", size=" (count arg)));定义变参函数  
  36. #'user/m  
  37. user=> (m 1 2 3 4 5)  
  38. "(1 2 3 4 5), size=5"  
  39. user=> (m "a" 1 2.3 -1)  
  40. "(\"a\" 1 2.3 -1), size=4"  
  41. user=> (defn exp [a f1 b f2 c] (f2 (f1 a b) c));函数作为参数  
  42. #'user/exp  
  43. user=> (exp 5 - 2 + 3)  
  44. 6  
  45. user=> (defn f [a] (fn [b] (- a b)));函数作为返回值  
  46. #'user/f  
  47. user=> ((f 74)  
  48. 3  


defn-: 
defn-与defn功能一致,都是用于定义函数的,但是defn-定义的函数作用域是私有的,而defn定义的函数是公有的,如下: 
Java代码  收藏代码
  1. user=> (ns test1);ns的意思是切换到指定的命名空间,如果不存在,则新建该命名空间  
  2. nil  
  3. test1=> (defn- foo [] "world");定义私有函数foo,返回字符串world  
  4. #'test1/foo  
  5. test1=> (defn bar [] (str "hello " (foo)));定义公有函数bar,并调用私有函数foo  
  6. #'test1/bar  
  7. test1=> (foo);当前命名空间内调用foo函数  
  8. "world"  
  9. test1=> (bar);当前命名空间内调用bar函数  
  10. "hello world"  
  11. test1=> (ns test2);切换到test2命名空间中  
  12. nil  
  13. test2=> (test1/bar);调用test1命名空间的bar函数,返回成功  
  14. "hello world"  
  15. test2=> (test1/foo);调用test1命名空间的foo函数,出现异常,提示test1的foo函数不是公开的  
  16. CompilerException java.lang.IllegalStateException: var: #'test1/foo is not public, compiling:(NO_SOURCE_PATH:1)  


组合函数comp: 
形如: 
Java代码  收藏代码
  1. ((comp f1 f2 .. fn) arg1 arg2 .. argn)  

就是对参数从右到左组合执行所有函数,可以转变为: 
Java代码  收藏代码
  1. (f1 (f2 (.. (fn arg1 arg2 .. argn))))  

举例如下: 
Java代码  收藏代码
  1. user=> (defn f [x y] (- (* x y)));使用defn定义函数方式  
  2. #user/f  
  3. user=> (f 2 4)  
  4. -8  
  5. user=> (def fc (comp - *));使用comp定义组合函数方式  
  6. #user/fc  
  7. user=> (fc 2 4)  
  8. -8  


偏函数partial: 
形如: 
Java代码  收藏代码
  1. ((partial  f  arg1 arg2 .. argn)  arga argb .. argz)  

就是执行: 
(f  arg1 arg2 .. argn  arga argb .. argz) 
注意:偏函数的第一个参数是一个函数,后面至少有1个其他参数 
partial函数称为“偏函数”或者“部分完整函数”,因为它是不完整的,定义也用def而不是defn。 
Java代码  收藏代码
  1. user=> (defn f [n] (* n 10));正常函数  
  2. #'user/f  
  3. user=> (f 2)  
  4. 20  
  5. user=> (def fp (partial * 10));偏函数  
  6. #'user/fp  
  7. user=> (fp 2)  
  8. 20  


constantly函数: 
constantly函数接受一个参数x,并返回一个变参函数,该变参函数无论参数是什么,都返回这个x值。 
Java代码  收藏代码
  1. user=> (def consf (constantly "a"))  
  2. #'user/consf  
  3. user=> (consf 1 2 3)  
  4. "a"  
  5. user=> (consf "a")  
  6. "a"  
  7. user=> (consf [1 2 3])  
  8. "a"  


二、函数调用 
->: 
宏-> 我们也称为 “thread” 宏, 它本质上是调用一系列的函数,前一个函数的返回值作为后一个函数的参数,返回最后一次函数调用的值. 比如下面两行代码的作用是一样的: 
Java代码  收藏代码
  1. user=>(first (.split (.replace (.toUpperCase "a b c d""A" "X"" "))  
  2. user=>"X"   
  3. user=> (-> "a b c d" .toUpperCase (.replace "A" "X") (.split " ") first)  
  4. user=>"X"   

这样调用的好处是更少的(),也更接近于scala的习惯。 
Java代码  收藏代码
  1. (-> (/ 144 12) (/ 2 3) str keyword list)  
  2. (list (keyword (str (/ (/ 144 122 3))))  

上面两句结果一样。 

->>: 
后面的函数迭代使用之前的函数结果作为最后一个参数,返回最后一次函数调用的值 
分下下面两个语句: 
Java代码  收藏代码
  1. (-> 10 (/ 3)) ; 10/3  10作为/函数第一个参数  
  2. (->> 10 (/ 3)) ; 3/10  10作为/函数最后一个参数  


eval: 
eval解析表达式数据结构(不是字符串),并返回结果。 
Java代码  收藏代码
  1. user=> (eval (str "(println 1)"));str函数返回字符串  
  2. "(println 1)"  
  3. user=> (read-string "(println 1)");而read-string函数用于从字符串中读取对象  
  4. (println 1)  
  5. user=>  (eval (read-string "(println 1)"))  
  6. 1  


apply函数: 
apply 把给定的集合里面的所有元素一次性地给指定的函数作为参数调用,然后返回这个函数的返回值。可以把apply看作是SQL里面的聚合函数,如下: 
Java代码  收藏代码
  1. user=> (apply + [1 2 3 4])  
  2. 10  


三、函数检查 
fn?: 
fn?用于检查给定的参数是否为函数,是返回true,否则返回false,如: 
Java代码  收藏代码
  1. user=> (fn? #("test"))  
  2. true  
  3. user=> (fn? +)  
  4. true  
  5. user=> (fn? 1)  
  6. false  
0 0
原创粉丝点击