What is the Doseq statement in Clojure?

Overview

A Doseq statement is a loop statement like the for-each loop statement in another programming language.

Loop

A loop constantly executes a code block until a condition is satisfied.

The Doseq statement

A Doseq statement iterates over a sequence of data.

Syntax

(doseq (sequence)
   statement#1)

Figure

A Doseq diagram

Example

(ns clojure.examples.hello
(:gen-class))
;; This program displays Hello World
(defn doseqq []
(doseq [n [0 1 2 6 7 3 6]]
(println n)))
(doseqq)

Explanation

  • Line 5: We create a function doseqq to demonstrate the Doseq loop.

  • Line 6: We declare Doseq and pass the sequence of values we want it to loop through.

  • Line 7: We print all the values of the n sequence.

  • Line 8: We call the doseqq function we created.

Free Resources