Você está na página 1de 2

Function calls use lists to define the call.

Lists are comprised of an opening a nd closing parenthesis, and zero or more elements. Each element is separated by whitespace or commas. Lists can be both a data structure and a functional call form. When used as a fu nction call, the first element of the lists is a function. Any remaining element s in the list are the function s arguments. When a list is used to perform an operation, it is called a form. The function e lement of the form is referred to as the operator. Strictly, it isn t always a fun ction. There are three kinds of forms: functions, macros, and special forms. Although the list can be used to perform operations, it can also be used to repr esent data. There is an important consequence of code and data sharing the same syntax: code can be manipulated as if it were data because it is data. One was to use lists simply as data is to use Clojure s built-in operation, list: user=> (list 1 2 3) (1 2 3) user=> (list a b c) (a b c) user=> (list "one" "two" "three") ("one" "two" "three") List items can be atoms, other lists, or other data structures that are part of Clojure. user=> (list :foo (list 1 2 3) [4 5 6]) (:foo (1 2 3) [4 5 6]) Clojure also has a short-cut syntax for creating a list as data. Just prepend th e list with a single-quote character: user=> '(1 2 3) (1 2 3) Creating a list this way has a slightly different effect. The list items are lef t unevaluated. The can be undefined, and Clojure won t complain. Using some of Clojure s built-in operations, information can be extracted from the data. The following expression returns the first element of the given list. user=> (first '("one" "two" "three")) "one" Another operation returns all the elements except the first: user=> (rest '("one" "two" "three")) ("two" "three") Vectors The vector is another data structure, similar to a list. Vectors are zero-based arrays. They can contain any value, and any mix of value types, just like lists. Here are a few examples: user=> [1 2 3] [1 2 3] user=> [:a 0 "hello"] [:a 0 "hello"] user=> [] [] The nice thing about vectors is you don t have to do anything special to use them

as data as you do with lists. Other languages give you a handy syntax for getting an array s element by its inde x. So, how is this accomplished with Clojure s vectors? Perform an operation: user=> ([7 8 9] 2) 9 Here, we fetch the value at index 2, which is 9. The vector itself is the operat or of the form. Its argument is 2. The general form for this operation is (vecto r index). Compare this with JavaScript, which is similar: [7, 8, 9][2]

Você também pode gostar