LISP – Macros ”; Previous Next Macros allow you to extend the syntax of standard LISP. Technically, a macro is a function that takes an s-expression as arguments and returns a LISP form, which is then evaluated. Defining a Macro In LISP, a named macro is defined using another macro named defmacro. Syntax for defining a macro is − (defmacro macro-name (parameter-list)) “Optional documentation string.” body-form The macro definition consists of the name of the macro, a parameter list, an optional documentation string, and a body of Lisp expressions that defines the job to be performed by the macro. Example Let us write a simple macro named setTo10, which will take a number and set its value to 10. Create new source code file named main.lisp and type the following code in it. Live Demo (defmacro setTo10(num) (setq num 10)(print num)) (setq x 25) (print x) (setTo10 x) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is − 25 10 Print Page Previous Next Advertisements ”;
Category: lisp
LISP – Environment
LISP – Environment Setup ”; Previous Next Local Environment Setup If you are still willing to set up your environment for Lisp programming language, you need the following two softwares available on your computer, (a) Text Editor and (b) The Lisp Executer. Text Editor This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi. Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX. The files you create with your editor are called source files and contain program source code. The source files for Lisp programs are typically named with the extension “.lisp“. Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, finally execute it. The Lisp Executer The source code written in source file is the human readable source for your program. It needs to be “executed”, to turn into machine language so that your CPU can actually execute the program as per instructions given. This Lisp programming language will be used to execute your source code into final executable program. I assume you have basic knowledge about a programming language. CLISP is the GNU Common LISP multi-architechtural compiler used for setting up LISP in Windows. The windows version emulates a unix environment using MingW under windows. The installer takes care of this and automatically adds clisp to the windows PATH variable. You can get the latest CLISP for Windows from here – https://sourceforge.net/projects/clisp/files/latest/download It creates a shortcut in the Start Menu by default, for the line-by-line interpreter. How to use CLISP During installation, clisp is automatically added to your PATH variable if you select the option (RECOMMENDED) This means that you can simply open a new Command Prompt window and type “clisp” to bring up the compiler. To run a *.lisp or *.lsp file, simply use − clisp hello.lisp Print Page Previous Next Advertisements ”;
LISP – Variables
LISP – Variables ”; Previous Next In LISP, each variable is represented by a symbol. The variable”s name is the name of the symbol and it is stored in the storage cell of the symbol. Global Variables Global variables have permanent values throughout the LISP system and remain in effect until a new value is specified. Global variables are generally declared using the defvar construct. For example Live Demo (defvar x 234) (write x) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is 234 Since there is no type declaration for variables in LISP, you directly specify a value for a symbol with the setq construct. For Example ->(setq x 10) The above expression assigns the value 10 to the variable x. You can refer to the variable using the symbol itself as an expression. The symbol-value function allows you to extract the value stored at the symbol storage place. For Example Create new source code file named main.lisp and type the following code in it. Live Demo (setq x 10) (setq y 20) (format t “x = ~2d y = ~2d ~%” x y) (setq x 100) (setq y 200) (format t “x = ~2d y = ~2d” x y) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is. x = 10 y = 20 x = 100 y = 200 Local Variables Local variables are defined within a given procedure. The parameters named as arguments within a function definition are also local variables. Local variables are accessible only within the respective function. Like the global variables, local variables can also be created using the setq construct. There are two other constructs – let and prog for creating local variables. The let construct has the following syntax. (let ((var1 val1) (var2 val2).. (varn valn))<s-expressions>) Where var1, var2, ..varn are variable names and val1, val2, .. valn are the initial values assigned to the respective variables. When let is executed, each variable is assigned the respective value and lastly the s-expression is evaluated. The value of the last expression evaluated is returned. If you don”t include an initial value for a variable, it is assigned to nil. Example Create new source code file named main.lisp and type the following code in it. Live Demo (let ((x ”a) (y ”b)(z ”c)) (format t “x = ~a y = ~a z = ~a” x y z)) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is. x = A y = B z = C The prog construct also has the list of local variables as its first argument, which is followed by the body of the prog, and any number of s-expressions. The prog function executes the list of s-expressions in sequence and returns nil unless it encounters a function call named return. Then the argument of the return function is evaluated and returned. Example Create new source code file named main.lisp and type the following code in it. Live Demo (prog ((x ”(a b c))(y ”(1 2 3))(z ”(p q 10))) (format t “x = ~a y = ~a z = ~a” x y z)) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is. x = (A B C) y = (1 2 3) z = (P Q 10) Print Page Previous Next Advertisements ”;
LISP – Data Types
LISP – Data Types ”; Previous Next In LISP, variables are not typed, but data objects are. LISP data types can be categorized as. Scalar types − for example, number types, characters, symbols etc. Data structures − for example, lists, vectors, bit-vectors, and strings. Any variable can take any LISP object as its value, unless you have declared it explicitly. Although, it is not necessary to specify a data type for a LISP variable, however, it helps in certain loop expansions, in method declarations and some other situations that we will discuss in later chapters. The data types are arranged into a hierarchy. A data type is a set of LISP objects and many objects may belong to one such set. The typep predicate is used for finding whether an object belongs to a specific type. The type-of function returns the data type of a given object. Type Specifiers in LISP Type specifiers are system-defined symbols for data types. array fixnum package simple-string atom float pathname simple-vector bignum function random-state single-float bit hash-table ratio standard-char bit-vector integer rational stream character keyword readtable string [common] list sequence [string-char] compiled-function long-float short-float symbol complex nill signed-byte t cons null simple-array unsigned-byte double-float number simple-bit-vector vector Apart from these system-defined types, you can create your own data types. When a structure type is defined using defstruct function, the name of the structure type becomes a valid type symbol. Example 1 Create new source code file named main.lisp and type the following code in it. Live Demo (setq x 10) (setq y 34.567) (setq ch nil) (setq n 123.78) (setq bg 11.0e+4) (setq r 124/2) (print x) (print y) (print n) (print ch) (print bg) (print r) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is − 10 34.567 123.78 NIL 110000.0 62 Example 2 Next let”s check the types of the variables used in the previous example. Create new source code file named main. lisp and type the following code in it. Live Demo (defvar x 10) (defvar y 34.567) (defvar ch nil) (defvar n 123.78) (defvar bg 11.0e+4) (defvar r 124/2) (print (type-of x)) (print (type-of y)) (print (type-of n)) (print (type-of ch)) (print (type-of bg)) (print (type-of r)) When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is − (INTEGER 0 281474976710655) SINGLE-FLOAT SINGLE-FLOAT NULL SINGLE-FLOAT (INTEGER 0 281474976710655) Print Page Previous Next Advertisements ”;