浏览 1139 次
锁定老帖子 主题:Clojure for
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2016-09-22  

 

for
(for seq-exprs body-expr)
 
列表内涵逻辑,接收一组(一个或多个)绑定form(binding-form)/集合表达式(collection-expr)对,每个绑定form(binding-form)/集合表达式(collection-expr)对后面跟着0个或多个modifier,最后获得一个求值表达式表示的惰性序列。
 
这里的seq-exprs形式为:
[binding-formcollection-expr
 modifier modifier modifier]
 
<!--[if ppt]--><!--[endif]-->或者多个的表现形式为: 
[binding-formcollection-expr
 modifier modifier modifier
 binding-formcollection-expr
 modifier modifier modifier
 
 binding-formcollection-expr
 modifier modifier modifier]
 
<!--[if ppt]--><!--[endif]-->例子: 
user=> (for [x [0 1 2 3 4 5]
  #_=>       :let [y (* x 3)]
  #_=>       :when (even? y)]
  #_=>   y)
(0 6 12)
 
user=> (for [x '(0 1 2 3 4 5)
  #_=>     :let [y (* x 3)]
  #_=>     :when (even? y)]
  #_=>  y)
(0 6 12)
<!--[if ppt]--><!--[endif]-->
 
再来个例子:
user=> (for [x [0 1 2 3 4 5]
  #_=>       y [0 1 2 3]
  #_=>       :let [z (* x y)]]
  #_=>   z)
(0 0 0 0 0 1 2 3 0 2 4 6 0 3 6 9 0 4 8 12 0 5 10 15)
 
 
再来个例子:
user=> (for [x [0 1 2 3 4 5]
  #_=>       :let [x (* x 2)]
  #_=>       y [0 1 2 3]
  #_=>       :let [z (* x y)]]
  #_=>   z)
(0 0 0 0 0 2 4 6 0 4 8 12 0 6 12 18 0 8 16 24 0 10 20 30)
 
 
 
 
 
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics