Explanation of common clojure Problems

Source: Internet
Author: User

The following content appears in the document on the official website of clojure. This article just summarizes common questions.

1. namespace, Lib, and code file conventions. See the following code:

(ns com.my-company.clojure.examples.my-utils  (:import java.util.Date)  (:use [clojure.contrib.def :only (defvar-)])  (:require [clojure.contrib.shell-out :as shell]))

Lib is represented as a Java resource. If you have written Java code, you should be familiar with the following directory structure:

myprj----src----resources--------com------------my_company----------------clojure--------------------examples------------------------my_utils.clj

This is the corresponding relationship. Note that dash (-) is converted into underscore (_). The suffix is CLJ.

NS is a macro, Which means:

Macro processes the form without eval and eval the returned result.

Because of this, although these free symbrs in NS are not bound, there will be no errors. If you use the following in repl:

(import java.util.Date)(use [clojure.contrib.def :only (defvar-)])(require [clojure.contrib.shell-out :as shell])

An error will occur, because import, use, and require are all functions, and the symbol java. util. date cannot be parsed.

2. List, (1 2 3)

This list must be confusing to some people. Because clojure gives it extra meaning.

(MAP # (+ 1%) (); this can be (MAP # (+ 1%) (1 2 3); this error

An empty list () evaluates to an empty list.
Non-empty lists are consideredCILSTo either special forms, macros, or functions. A call has the form (operator operands *).

An empty list. The result is an empty list.

If the list is not empty, it is called, special form, Macro, and function. 1 is not one of the three, so an error occurs. The following will not cause errors.

(MAP # (+ 1%) '(1 2 3); a single quotation mark or reverse single quotation mark (MAP # (+ 1%) (List 1 2 3) is added to the front ));


Back to the previous topic, why can I use it like this?

(import ‘java.util.Date)

Since the import is a function, there is always a place to parse this java. util. Date? Your understanding of symbol is involved here. This is a bit difficult to describe. I have to explain it using examples.

You can see X in the code. This X may be a var, ref, agent, or atom, regardless of it. How does colure describe this X itself? It is X rather than Y. All of the above mentioned are var, ref, and so on.

(X 1 2); this X may be a function, macro @ X; this X may be ref, agent, Atom, promise, future, etc. (+ x 1 ); X should be a VaR

For ease of understanding, we will also introduce the dispatch of clojure reader. When reader starts with #, it will understand the following content in another way.

(def x 5)#‘x

In the code above, X is a VaR with a value of 5. This VaR is also an object. What if we get this object? # The value of VaR represented by symbol x obtained by 'X. In normal parsing, reader parses X into its value.

[1 x] ----> [1 5]

With this in mind, the acquisition of symbol itself is:

'X note that X can be uninitialized and does not represent anything. 'x, that is, I am the symbol X.

The symbol can be converted to a string.

(Name 'x) to get "X ".

The above Code indicates that my name is X.

Therefore, after the above use function accepts the escape parameters, it passes various conversions and is finally delivered to reader to see the string. That is, you and I are the most familiar to programmers.

Explanation of common clojure Problems

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.