Common Lisp Getting Started note (ii) Assignment and input and output

Source: Internet
Author: User
Tags print format setf

From the language of Lisp-Chen Guangxi

First, assign a value

(1) Let

Lisp uses let to complete the definition of a local variable. The form is: (Let (var1 exp1) (var2 exp2) ... (Varn expn))
Exps

Where the operator let indicates that a local variable will be defined. (Vari expi) vari the variable name, the initial value of which is the value of the expression Expi. The exps part of the Let statement is the expression collection that completes the required processing. The variables defined by Var1,...,varn are valid in these expressions, that is, in the Let statement body. The return value of the entire let statement is the value of the last statement in the statement body. Such as:

> (Let ((a 1) (b 2) (C (+ 5 3))
(+ A (* b c))
(-(* a C) (* b c)))
>-8


(2) Defparameter

Defparameter is used to implement global variable definitions. General conventions The naming of global variables is to write an * number on each side, such as:

> (defparameter *var* 123)
>*var*


(3) Defconstant

Defconstant is used to define global constants, such as:

> (defconstant C 12)
>c

To determine whether a symbol is defined as a global variable or constant, you can use BOUNDP to test it. For example:

> (BOUNDP ' C)
>t


(4) SETF

SETF is a common assignment operator. The first time you use a setf assignment for a symbol (not defined as a local variable), the symbol is used by the system as a global variable. This is a method of implicitly defining global variables, but it is not worth advocating.

> (setf *var* 1234)

>1234


Ii. Input and output

(1) Common Lisp The most commonly used output command is format. Use the following:

> (Format T "~%the Clever dog can do ~a + ~a =~a." 2 35)
The clever dog can do 2 + 3 = 5.
NIL
The first parameter of the Fortmat is the output, the parameter t of this example means output to toplevel; The second argument is the output format string, ~%
Represents a newline, and ~a indicates that the location will be replaced with the corresponding parameter. Common Lisp has a number of control characters in the print format string, such as:
The ~a,~s,~p is used for object control. For example
> (Format T "~%tildes prints ~s~%tildea prints ~a" ' Acl::asymbol ' Acl::asymbol)
Tildes Prints Acl::asymbol
Tildea Prints Asymbol
NIL
This example should pay attention to the difference between ~a and ~s. ~s can print out the control, and ~a cannot. ~p print the plural suffix "s".
~d ~b ~o ~x ~r are integer controls that print decimal, binary, octal, 16, cardinality, respectively.
> (Format nil "~d ~b ~o ~x ~r" 12 12 12 12 12)
"1100 C Twelve"
~e ~f ~g ~$ are floating-point control characters, respectively, according to the exponential format, fixed-point, floating point, floating point fixed-point combination. Cases
> (Format nil "~e ~f ~g ~$" 123450 123450 123450 123450)
"1.2345E5 123450.0 123450. 123450.00 "
> (Format nil "~e ~f ~g ~$" 123.450 123.450 123.450 12.3450)
"1.2345E2 123.45 123.45 12.35"
~n%,~n&,~| is a blank control, representing n wrapping, n-1 lines, and a new page.
> (Format nil "begin ~2% and ~2&end")
The Begin


and


End "
It is worth noting that in the above example, when the first parameter of format is nil, the return value does not appear nil after the print execution, and the parameter
When T, the return value appears after print execution nil> (setf A (READ)) 12
12, this is the so-called Lisp byproduct.


(2) The standard input operator for Common Lisp is read, such as:

> (setf A (READ))
>12


Common Lisp Getting Started note (ii) Assignment and input and output

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.