Open In App

Packages in LISP

Last Updated : 15 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Packages are a central mechanism in which a   Common Lisp is used in managing different sets of names and avoiding name collisions that may occur if multiple files contain the same variables or functions with the same name. Understanding the package system to correctly split our code across multiple files and then load them together as one program. Packages facilitate Common Lisp to divide namespace.

A package is a Lisp object like a number, a list, or a character string. In particular, the type package is defined. A package can be the value of a symbol, given as an argument to a function, returned by a function, etc.

 Syntax for Creating a LISP Package:

(defpackage :package-name
  (:use :Function1 ...)
  (:export :Geek1 :Geek2 ...)
)

Where, 

  • defpackage: This package is used to create a user to define the package.
  • package-name: It defines the name of the package created by the user.
  •  :use: This Function is used in the code by the package.
  • :export: represents an external package.
make-package package-name &key :StudentsNames :use

make-package is used in creating user-defined packages.

 

Example 1:

Lisp




/* Making a Greeting Package 
(make-package :Ayush_Agarwal)
(make-package :Piyush_Agarwal)
(make-package :Keshav_Agarwal)
(in-package Ayush_Agarwal)
(defun greet () 
  (write-line "This is Ayush_Agarwal ,GoodMorning")
)
(greet)
(in-package Piyush_Agarwal)
(defun greet () 
  (write-line "This is Piyush_Agarwal ,GoodMorning")
)
(greet)
(in-package Keshav_Agarwal)
(defun greet () 
  (write-line "This is Keshav_Agarwal , GoodMorning")
)
  
(in-package Ayush_Agarwal)
(in-package Piyush_Agarwal)
(in-package Keshav_Agarwal)
(greet)


Output:

 

For Deleting a LISP package we use the following syntax.

Syntax:

(delete-package Package-name)
  • delete-package: It allows us to delete a user-defined package.

Example 2:

Lisp




/*  Deleting a user defined package in main.lisp-
(make-package :Ayush_Agarwal)
(make-package :Piyush_Agarwal)
(make-package :Keshav_Agarwal)
(in-package Ayush_Agarwal)
(defun greet () 
 (write-line "This is Ayush_Agarwal ,GoodMorning")
)
(greet)
(in-package Piyush_Agarwal)
(defun greet () 
 (write-line "This is Piyush_Agarwal ,GoodMorning")
)
(greet)
(in-package Keshav_Agarwal)
(defun greet () 
 (write-line "This is Keshav_Agarwal ,GoodMorning")
)
(greet)
(in-package Ayush_Agarwal)
(delete-package Ayush_Agarwal)
(greet)
(in-package Piyush_Agarwal)
(greet)
(in-package Keshav_Agarwal)
(greet)


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads