Clojure Grammar Learning-Looping

Source: Internet
Author: User

DO and block statements

In Scala, statements enclosed in curly braces {} form a block, and its value is the value of the last statement.

Scala> val a = {     | println ("a")     | 1}aa:int = 1

{println ("a"); 1} has a value of 1.

In Clojure, it is sometimes necessary to make multiple form blocks, and the value of this block is the value of the last form. Just use do

User=> (Def A (Do (println "a") 1)) a# ' user/auser=> A1

Do takes any number of forms, evaluates them all, and returns the last.

Do accepts any number of form parameters, evaluates them individually, and then returns the value of the last form.

Cycle

What are the elements that make up a loop?

In Java

    1. First, we need to provide a statement that will be executed in each loop--the loop body
    2. If it is not an infinite loop, we need to provide an exit condition that, when this condition is met, no longer loops.

In the Clojure

    1. First, we need to provide a statement that will be executed in each loop--the loop body
    2. We need to provide a loop condition. When this condition is met, the next cycle continues.

This means that Java needs us to tell it when to exit the loop, and Clojure needs us to tell it when to continue the loop.

This is the Java for loop.

for (int i = 0; i < i++)    System.out.println (i);

It can be considered that i<, i++ and System.out.println (i) Form a cyclic body. Exit condition is i<10.

A while loop differs from a for loop in that while cannot declare a variable that is used only inside a loop. In the For loop above, I is used only inside the loop. If we want the while to have a similar function (of course, while without this function), then the while needs to accept an initialization syntax that becomes

while (int i = 0) (i <) {    println (i);    i++;}

In Clojure, it is also possible to provide initialization statements in the form of a binding, as well as to provide a loop body. This is done through the form of loop.

(Loop [bindings *] exprs*)

This is similar to the one in front of this enhanced version. At the same time, a break is required in the while loop, and in Clojure we need a form to continue the loop, which is recur. You can assume that the Java loop is active, and that the clojure is passive, and you have to drive it forward in your code.

(Loop [a 0] (if (< a) (Do (println a) (recur (+ 1 A))))

Recur causes the program to restart the loop execution. But how to simply re-execute loops in a program is just stepping in place, because all bindings are always the initial values. So recur not only transforms the execution flow of the program, but also modifies the binding that the loop begins with . That is, recur allows loop to begin binding to a (+ 1 a).

If we provide more than one binding at the beginning of the loop

(Loop [a 0 B 1] (if (< a) (Do (println a) (recur (+ 1 A))))

REPL will tell us that the number of arguments supplied to recur is incorrect.

Compilerexception java.lang.IllegalArgumentException:Mismatched argument count to recur, Expected:2 args, got:1

In fact, recur can be used not only for loops, but also for functions , which allows the function to be re-executed.

Give me an example of a book.

(Defn countdown [result X] (if (zero x) result (recur (conj result x) (Dec x)))

Execution (count down [] 5) outputs the return value [5 4 3 2 1]

Why does this code look so familiar? Doesn't that look like a tail-recursion?

Clojure Grammar Learning-Looping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.