Basic Introduction to Clojure (i)

Source: Internet
Author: User

Recently watching Storm's source code, learn to share the next Clojure grammar.

Read the catalogue:

    1. Overview
    2. Variable
    3. Operator
    4. Process Control
    5. Summarize
Overview

Clojure is a Lisp dialect running on the JVM, which is a functional programming paradigm, and it can be easily invoked with Java, so that the developed program can combine the advantages of Java and Clojure (Storm), just like the roles of C # and F # in net.

Install the JDK above java1.6.
Download the Clojure Environment pack.
After decompression, use the command line to enter the Clojure directory, enter the following command to enter the REPL command line interactive interface:

JAVA-CP clojure-1.7. 0. Jar Clojure.main

Welcome into the world of Clojure.

Variable

Variables in Clojure can be declared with the DEF keyword:


The Clojure data type is weakly typed, corresponds to object in Java, and is then called directly through the variable name.

Similar to the code in C #:

Object " Good Morning " ;  Console.WriteLine (greet);

The Clojure syntax is to enclose the code elements in parentheses, separating the elements with spaces, so that they write code like a fill list.

" Hello world! ")

In functional programming, functions are the first class of objects, and the first element after the parentheses is treated as a function or a macro by the Clojure interpreter, after which it is treated as a parameter, as above "Hello World" is passed as a parameter to the STR function for output.

Similar to C # code:

Console.WriteLine ("Hello world! ");

A variable can be assigned repeatedly, or it can be of different types:


Def can assign a function to a variable, the function variable cannot be called directly, and parentheses are required:


Similar to C # code:

Action func = () = {Console.WriteLine ("HelloWorld");}; Func ();
Operator

Clojure uses the predecessor operator, a semicolon (;) indicates that followed by a comment.
Subtraction

1 1 2 (211(122(2  12

Increment, accept a value plus 1 after the range, similar to i++ in C #.

5);=>6

Weight reduction:

5);=>4

Quotient function:

5 2);=>2

To find the remainder function:

5 2);=>1

Less Than (<):

5 6);=>true

Less than or equal to (<=):

5 5); true

Greater than (>):

8 5);=>true

Greater than or equal to (>=):

5 5); true

Equal sign =

1 1 true (21false(1"1" false

The same applies to the predecessor operator as a function, followed by a parameter.

Function

In Clojure, a function is the first class object, and the first element after its parentheses is treated as a function or a macro by the Clojure interpreter, which is then treated as a parameter. Define functions with Defn, similar to the def that defines variables:

" Hello World ") ; ="HelloWorld"

The following defines the Get function, in which the parentheses are the parameters of the function, and the output Hello World string. After the parameter is added, the call needs to pass in the value again if the exception is reported.

The A function parameter is used inside the function, and if you want to splice it, you can use the STR function internally, using the value returned as the parameter of the Get function:

Multiple parameter representations: [ARG1,ARG2]

Process Control judgment if

There are three layers of nesting, first (= age 18) to determine whether the return is equal to 18, as if to determine which string to return based on, and then based on if the string as the return value of the function. Note that if is not a function or a macro, but rather is called special form (special form), and there is no else, the else is represented by whitespace or newline, without explicit return,clojure the last represented value as the return value of the function.

predicate functions

A predicate function is a judgment, a function or functor that returns a bool value, to test whether the condition is true or not.
With or non (and or not):

There is a rule in Clojure (Lisp habit): For functions that Judge functions, there is a function name followed by a "? No. So as long as you see the name of the function with a question mark in the back, you know it must be a judgment statement.

User=> (FN 1); Judge 1 is not a function false

A predicate that tests the type of an object: Class?,coll?,decimal?,delay?,float?,fn?,instance?,integer?,isa?,keyword?,list?,macro?,map?,number ?, Seq?,set?,string?,vector?.

The predicates that test the relationship between the two values are: <,<=,=,not=,==,>,>=,compare,distinct? , identical?

The predicates that test the logical relationship are: And,or,not,true?,false?,nil?.

The predicates that test numbers are: Even?,neg?,odd?,pos?,zero?.

Summarize

Each operation within the Clojure is implemented in the following three forms:

    • Functions (function) are defined as defn in the text.
    • Almost all of the functions and macros in macro Clojure are implemented with Clojure code, which is followed by a macro.
    • The If that appears in the special form text, the other includes Catch,def,do,dot ('. '), Finally,fn,if,let,loop,monitor-enter,monitor-exit,new,quote, Recur,set!,throw,try,var.

The blog Park code highlighting does not support Clojure.

[0] http://clojure.org/[1] http://xumingming.sinaapp.com/302/[2] http://huangz.iteye.com/blog/1325228[3] Http://segmentfault.com/a/1190000000414279#articleHeader0

Basic Introduction to Clojure (i)

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.