如何打印列表中的每一项在单独的行clojure? [英] How to print each item of list in separate line clojure?

查看:150
本文介绍了如何打印列表中的每一项在单独的行clojure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是clojure的新手。我想在换行符中打印每个列表项。我试着这样:

I'm very new in clojure. I want to print each item of list in newline. I'm trying like this:

user=> (def my-list '(1 2 3 4 5 ))
;; #'user/my-list
user=> my-list
;; (1 2 3 4 5)
user=> (apply println my-list)
;; 1 2 3 4 5
;; nil

但我希望我的输出必须是:

But I want my output must be:

1
2
3
4
5
nil

任何人都可以告诉我,我该怎么办?感谢。

can anyone tell me, How can I do this? Thanks.

推荐答案

这种用法(对序列的每个成员执行一次副作用) a href =https://www.conj.io/store/v1/org.clojure/clojure/1.8.0/clj/clojure.core/doseq =nofollow> doseq 。使用它在这里将是像

This kind of use case (perform a side effect once for each member of a sequence) is the purpose of doseq. Using it here would be like

(doseq [item my-list]
   (println item))

请注意,这不会打印 nil ,但会返回。在REPL中使用它将看到打印的所有表达式的返回值,但不会发生在例如。启动您的项目作为终端程序。

Note that this will not print nil but will return it. Working with it in the REPL will see the return values of all expressions printed, but doesn't happen in e.g. starting your project as a terminal program.

这篇关于如何打印列表中的每一项在单独的行clojure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆