This document provides an overview of the Lisp programming language. It begins with some notable quotes about Lisp praising its power and importance. It then covers the basic syntax of Lisp including its use of prefix notation, basic data types like integers and booleans, and variables. It demonstrates how to print, use conditional statements like IF and COND, and describes lists as the core data structure in Lisp.
LISP, an acronym for list processing, is a programming language that was designed for easy manipulation of data strings. It is a commonly used language for artificial intelligence (AI) programming.
LISP, an acronym for list processing, is a programming language that was designed for easy manipulation of data strings. It is a commonly used language for artificial intelligence (AI) programming.
Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called "Data Structure". The choice of a particular data model depends on two considerations:
It must be rich enough in structure to reflect the actual relationships of the data in the real world.
The structure should be simple enough that one can effectively process the data when necessary.
Data Structure Operations
The particular data structure that one chooses for a given situation depends largely on the nature of specific operations to be performed.
The following are the four major operations associated with any data structure:
i. Traversing : Accessing each record exactly once so that certain items in the record may be processed.
ii. Searching : Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.
iii. Inserting : Adding a new record to the structure.
iv. Deleting : Removing a record from the structure.
Primitive and Composite Data Types
Primitive Data Types are Basic data types of any language. In most computers these are native to the machine's hardware.
Some Primitive data types are:
Integer
Also since {a} is regular, {a}* is a regular language which is the set of strings consisting of a's such as , a, aa, aaa, aaaa etc. Note also that *, which is the set of strings consisting of a's and b's, is a regular language because {a, b} is regular. Regular expressions are used to denote regular languages.
It is related to Analysis and Design Of Algorithms Subject.Basically it describe basic of topological sorting, it's algorithm and step by step process to solve the example of topological sort.
Knowledge representation and reasoning (KR) is the field of artificial intelligence (AI) dedicated to representing information about the world in a form that a computer system can utilize to solve complex tasks such as diagnosing a medical condition or having a dialog in a natural language
This presentation will introduce you to programming languages that support different programming paradigms and to give you the knowledge of basic concepts and techniques that will allow them to differentiate between various programming paradigms.
Artificial Intelligence: Introduction, Typical Applications. State Space Search: Depth Bounded
DFS, Depth First Iterative Deepening. Heuristic Search: Heuristic Functions, Best First Search,
Hill Climbing, Variable Neighborhood Descent, Beam Search, Tabu Search. Optimal Search: A
*
algorithm, Iterative Deepening A*
, Recursive Best First Search, Pruning the CLOSED and OPEN
Lists
Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called "Data Structure". The choice of a particular data model depends on two considerations:
It must be rich enough in structure to reflect the actual relationships of the data in the real world.
The structure should be simple enough that one can effectively process the data when necessary.
Data Structure Operations
The particular data structure that one chooses for a given situation depends largely on the nature of specific operations to be performed.
The following are the four major operations associated with any data structure:
i. Traversing : Accessing each record exactly once so that certain items in the record may be processed.
ii. Searching : Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.
iii. Inserting : Adding a new record to the structure.
iv. Deleting : Removing a record from the structure.
Primitive and Composite Data Types
Primitive Data Types are Basic data types of any language. In most computers these are native to the machine's hardware.
Some Primitive data types are:
Integer
Also since {a} is regular, {a}* is a regular language which is the set of strings consisting of a's such as , a, aa, aaa, aaaa etc. Note also that *, which is the set of strings consisting of a's and b's, is a regular language because {a, b} is regular. Regular expressions are used to denote regular languages.
It is related to Analysis and Design Of Algorithms Subject.Basically it describe basic of topological sorting, it's algorithm and step by step process to solve the example of topological sort.
Knowledge representation and reasoning (KR) is the field of artificial intelligence (AI) dedicated to representing information about the world in a form that a computer system can utilize to solve complex tasks such as diagnosing a medical condition or having a dialog in a natural language
This presentation will introduce you to programming languages that support different programming paradigms and to give you the knowledge of basic concepts and techniques that will allow them to differentiate between various programming paradigms.
Artificial Intelligence: Introduction, Typical Applications. State Space Search: Depth Bounded
DFS, Depth First Iterative Deepening. Heuristic Search: Heuristic Functions, Best First Search,
Hill Climbing, Variable Neighborhood Descent, Beam Search, Tabu Search. Optimal Search: A
*
algorithm, Iterative Deepening A*
, Recursive Best First Search, Pruning the CLOSED and OPEN
Lists
Functional Programming Past Present FutureIndicThreads
Presented at the IndicThreads.com Software Development Conference 2016 held in Pune, India. More at http://www.IndicThreads.com and http://Pune16.IndicThreads.com
--
An introduction to the basic concepts on functional programming, explaining why it is a hot topic for some years now, what it is and some suggestions of functional languages to be learned.
This is a talk from the March 2015 Austin Clojure Meetup in which I explored LISP 1.5 and showed how to run the original LISP 1.5 in an IBM 7094 simulator.
Natural Language Processing using JavaScript "Natural" Library. This deck covers Natural Language Understanding using JavaScript "Natural" library in detail
Discussed in detail about how to design and develop custom skills (think custom apps) for Amazon Alexa Voice service.
Discusses how to design voice based experiences in detail.
Workflows are a key component of server side of IoT solution along with Analytics, Rule Engine and IoT device management. IoT focused Workflow tools draw their inspiration of classical workflow tools that exist in market, but focus more on IoT use cases. For example they are able to connect with IoT devices using IoT specific protocols like CoAP or MQTT. Node-RED is a visual tool for wiring together hardware devices, APIs and online services in new and interesting ways. It’s build by IBM Emerging Technology team from group for IoT, though it’s not limited only to IoT.
Using Swift for all Apple platforms (iOS, watchOS, tvOS and OS X)Aniruddha Chakrabarti
Swift has gained widespread popularity in just an year. So much so that Swift have emerged as the de-facto standard programming language for all Apple platforms including iOS, watchOS, tvOS and OS X. Apple also open sources Swift and soon after IBM ported Swift to Linux. Swift incorporates the language innovations that have happened in the last two decades. Swift is a compiled programming language and belongs to the ‘C’ family of languages similar to C++, Java, C#, Objective-C and D. Swift is influenced by dynamic programming languages like Python, Ruby and functional programming languages like Haskell.
Future of .NET - .NET on Non Windows Platforms. .NET has been so far targeted towards Windows only. Now Microsoft created a subset of .NET called .NET Core that would run on Linux and OS X apart from Windows.
High level overview of CoAP or Constrained Application Protocol. CoAP is a HTTP like protocol suitable for constrained environment like IoT. CoAP uses HTTP like request response model, status code etc.
memcached Distributed Cache. memcached is the most popular cache solution for low latency high throughput websites. improves the read timings drastically.
Provides an overview of Redis which is a Key Value NoSQL database and the different data types it supports. Also shows how to use Redis Client API from node.
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
A presentation about the usage and availability of Varnish on Kubernetes. This talk explores the capabilities of Varnish caching and shows how to use the Varnish Helm chart to deploy it to Kubernetes.
This presentation was delivered at K8SUG Singapore. See https://feryn.eu/presentations/accelerate-your-kubernetes-clusters-with-varnish-caching-k8sug-singapore-28-2024 for more details.
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
As AI technology is pushing into IT I was wondering myself, as an “infrastructure container kubernetes guy”, how get this fancy AI technology get managed from an infrastructure operational view? Is it possible to apply our lovely cloud native principals as well? What benefit’s both technologies could bring to each other?
Let me take this questions and provide you a short journey through existing deployment models and use cases for AI software. On practical examples, we discuss what cloud/on-premise strategy we may need for applying it to our own infrastructure to get it to work from an enterprise perspective. I want to give an overview about infrastructure requirements and technologies, what could be beneficial or limiting your AI use cases in an enterprise environment. An interactive Demo will give you some insides, what approaches I got already working for real.
JMeter webinar - integration with InfluxDB and GrafanaRTTS
Watch this recorded webinar about real-time monitoring of application performance. See how to integrate Apache JMeter, the open-source leader in performance testing, with InfluxDB, the open-source time-series database, and Grafana, the open-source analytics and visualization application.
In this webinar, we will review the benefits of leveraging InfluxDB and Grafana when executing load tests and demonstrate how these tools are used to visualize performance metrics.
Length: 30 minutes
Session Overview
-------------------------------------------
During this webinar, we will cover the following topics while demonstrating the integrations of JMeter, InfluxDB and Grafana:
- What out-of-the-box solutions are available for real-time monitoring JMeter tests?
- What are the benefits of integrating InfluxDB and Grafana into the load testing stack?
- Which features are provided by Grafana?
- Demonstration of InfluxDB and Grafana using a practice web application
To view the webinar recording, go to:
https://www.rttsweb.com/jmeter-integration-webinar
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
💥 Speed, accuracy, and scaling – discover the superpowers of GenAI in action with UiPath Document Understanding and Communications Mining™:
See how to accelerate model training and optimize model performance with active learning
Learn about the latest enhancements to out-of-the-box document processing – with little to no training required
Get an exclusive demo of the new family of UiPath LLMs – GenAI models specialized for processing different types of documents and messages
This is a hands-on session specifically designed for automation developers and AI enthusiasts seeking to enhance their knowledge in leveraging the latest intelligent document processing capabilities offered by UiPath.
Speakers:
👨🏫 Andras Palfi, Senior Product Manager, UiPath
👩🏫 Lenka Dulovicova, Product Program Manager, UiPath
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Lisp
1. Aniruddha Chakrabarti
Associate Vice President & Chief Architect, Digital Practice, Mphasis
caniruddha@hotmail.com | in.linkedin.com/in/aniruddhac
Lisp (LISP)
2. The greatest single programming language ever designed.
Alan Kay, about LISP.
The most powerful programming language is Lisp. If you don't know Lisp (or its variant,
Scheme), you don't appreciate what a powerful language is. Once you learn Lisp you will
see what is missing in most other languages.
Richard Stallman [How I do my computing]
One of the most important and fascinating of all computer languages is Lisp, which was
invented by John McCarthy around the time Algol was invented.
Douglas Hofstadter, Gödel, Escher, Bach: an Eternal Golden Braid
Lisp is worth learning for the profound enlightenment experience you will have when you
finally get it; that experience will make you a better programmer for the rest of your days,
even if you never actually use Lisp itself a lot.
Eric S. Raymond, How to Become a Hacker
1958 - John McCarthy and Paul Graham invent LISP. Due to high costs caused by a post-
war depletion of the strategic parentheses reserve LISP never becomes popular...
Fortunately for computer science the supply of curly braces & angle brackets remains high.
Iry James, A Brief, Incomplete, and Mostly Wrong History of Programming Languages
Few remarkable quotes on Lisp
Source - http://www.paulgraham.com/quotes.html
3. Agenda
• What is Lisp, brief history and where Lisp is used
• Basic syntax, building blocks, prefix notation
• Data types
• Variables
• Constants
• IO (Read and Write/print)
• Control flow statements
• List and List comprehension
• Function and Lambda Expression
• Structure
4. What is Lisp
• Lisp (or LISP) is a family of computer programming languages with a long
history and a distinctive, fully parenthesized syntax.
• Invented in 1958 by John McCarthy
• Second-oldest high-level programming language in widespread use today;
only Fortran is older (by one year).
• Generally regarded as being the first functional programming language.
• Not a pure functional language as supports variable assignment.
• The name Lisp (historically LISP) derives from LISt Processor
• Multi paradigm – functional, procedural, reflective, meta
• First programming language that showed concepts and constructs of
functional programming
• Not an imperative programming language like C, C++, Java, C#
• Has many dialects, two of the following being most popular
– Common Lisp
– Scheme
5. History
• Invented by John McCarthy in 1958 while he was at MIT
• First implemented by Steve Russell on an IBM 704 computer.
• Common Lisp originated, during the 1980s and 1990s, in an attempt to unify
the work of several implementation groups.
Steve RussellJohn McCarthy
http://www.paulgraham.com/rootsoflisp.html
6. Where is Lisp (or it’s dialects) used
• AI (Artificial Intelligence)
• Academia / Education (in many CS courses)
– Structure and Interpretation of Computer Programs (MIT)
– DrRacket
• CAD (AutoCAD, CoCreate, and several others)
• Development environments (Emacs and others)
• Clojure a dialect of Lisp (targets the Java Virtual Machine, also CLR and
Javascript) has seen good acceptance in recent years
– http://dev.clojure.org/display/community/Clojure+Success+Stories
7. Lisp dialects & languages it influenced
• LISP 1 – First implementation.
• LISP 1.5 – First widely distributed version,
developed by McCarthy and others at MIT.
• Stanford LISP – This was a successor to LISP 1.5
developed at the Stanford AI Lab
• MACLISP – developed for MIT's Project MAC
• Common Lisp
• Scheme
• Emacs Lisp
• Clojure - a modern dialect of Lisp which
compiles to the Java virtual machine and
handles concurrency very well.
• Haskell
• Logo
• Lua
• Perl
• Python
• Racket
• JavaScript
• Ruby
• Smalltalk
• Tcl
• R
8. Lisp Program Structure
• LISP expressions are called symbolic expressions or s-
expressions. The s-expressions are composed of three valid
objects, atoms, lists and strings.
• Any s-expression is a valid program.
Example - (+ 1 2)
• LISP programs run either on an interpreter (REPL) or
as compiled code
• LISP expressions are case-insensitive, so the below lines are
same -
(write-line "Hello World")
(WRITE-LINE "Hello World")
• The basic numeric operations in LISP are +, -, *, and /
• LISP represents a function call f(x) as (f x), for example cos(45)
is written as (cos 45)
9. Basic Lisp Syntax
• Lisp has a different syntax than C like languages (C++, Java, C#)
• Uses parenthesis extensively - ( and )
• Uses prefix notation
For example the expression to add 1 and 2 would be (+ 1 2) in Lisp
Lisp C like languages
Math Operation (+ 1 2)
(+ 1 2 3 4)
1 + 2
1 + 2 + 3 + 4
Variable Assignment (defvar my-name "David") my-name = "David"
Invoking Functions (write-line "Hello World") printf("Hello World")
If, Else (if (> 2 3)
"bigger"
"smaller")
if (2 > 3){
"bigger"
}
else
"smaller"
}
Function definition (defun add (a b)
(+ a b))
int add(int a,int b){
return a + b;
}
10. Building Blocks of Lisp
• LISP programs are made up of three basic building blocks:
– Atom: An atom is a number or string of contiguous characters. It
includes numbers and special characters.
hello-from-lisp
name
123008907
*hello*
Block#221
Abc123
– List: Sequence of atoms and/or other lists enclosed in parentheses.
( i am a list)
(a ( a b c) d e fgh)
(father tom ( susan bill joe))
(sun mon tue wed thur fri sat)
( )
– String: Group of characters enclosed in double quotation marks.
" I am a string"
"a ba c d efg #$%^&!"
"Please enter the following details :"
"Hello from 'Tutorials Point'! "
12. Prefix / Polish Prefix notation
• No concepts of operators for primitive types
• Simple arithmetic operations also follow function calling syntax
function_name arg1 arg2... argN
operator arg1 arg2 ... argN
(+ 2 5)
7
(+ 2 5 3 4 9 20)
43
(+ 2.5 7.25)
9.25
13. Notations
Prefix Notation
(also called Polish
Notation or Polish Prefix
Notation)
places operators to the
left of their operands
operator arg1 arg2
+ 2 5
7
Infix Notation
operators are written
infix-style between the
operands they act on
arg1 operator arg2
2 + 5
7
Postfix Notation
(also called Reverse
Polish notation (RPN))
puts the operator in the
prefix position.
operator arg1 arg2
2 5 +
7
14. Comment
• A Lisp comment begins with a semi-colon.
; This is a Lisp comments - ignored by the interpreter.
(print "Hello World") ; this is an example of comment
; the following statement prints addition of 2 and 3
(print (+ 2 3))
• Supports multiple comment through #| … |#
#|
this is an example of multiple comment
as this comment is spread across multiple lines
|#
#| first line
this is an example of multiple comment
as this comment is spread across multiple lines
last line |#
15. Symbol
• Symbol is a name that represents data objects and interestingly
it is also a data object.
• A symbol is just a string of characters.
• There are restrictions on what you can include in a symbol and
what the first character can be, but as long as you stick to
letters, digits, and hyphens, you'll be safe.
17. Data types
• type-of function returns the data type of a given object
(defvar name "XYZ")
(defvar age 30)
(defvar salary 1234.567)
(defvar isMale T)
(print (type-of name))
(print (type-of age))
(print (type-of salary))
(print (type-of isMale))
(SIMPLE-BASE-STRING 3)
(INTEGER 0 281474976710655)
SINGLE-FLOAT
BOOLEAN
18. Declaring new variables (defvar)
• defvar – declares global dynamic variables. DEFVAR optionally sets it
to some value, unless it is already defined.
• defvar assigns a value only first time. variables are generally
immutable (meaning their value can not change)
(defvar my-name "David")
"David"
my-name
"David"
(defvar my-name "David")
(print my-name) ;prints “David”
(defvar my-name “Aniruddha") ;does not change the value of my-name
(print my-name) ;still prints “David”
"David"
"David"
(defvar a-variable 57)
57
a-variable
57
• In Scheme, define is used both for procedures and for
variables whose values aren't procedures.
• In Lisp (and Common Lisp), procedures are given
names by a mechanism separate from the general
variable mechanism; defun is only for procedures. To
define a variable, use defvar
19. Declaring new variables (defparameter)
• defparameter – also declares global dynamic variables similar
to defvar.
• defparameter always assigns a value (unlike defvar which
assigns the value only first time)
(defparameter my-name "David")
"David"
my-name
"David"
(defparameter my-name "David")
(print my-name)
(defvar my-name “Aniruddha") ;does change the value of my-name
(print my-name)
"David"
“Aniruddha"
20. Mutate existing variables (setq)
• SETQ's first argument is a symbol. This is not evaluated. The second argument is
assigned as the variable's value.
(setq my-name "David")
"David"
my-name
"David"
(setq a-variable 57)
57
a-variable
57
• SETQ form can actually take any even number of arguments, which should be
alternating symbols and values.
• SETQ performs the assignments from left to right, and returns the rightmost
value.
(setq year 2014 month "September" day 20)
(print year)
(print month)
(print day)
Output: 2014
"September"
20
21. Declaring Constants (defconstant)
• Constants are variables that never change their values during program
execution.
• Constants are declared using the defconstant construct.
(defconstant PI 3.14)
(print PI)
3.14
(setq PI 6.54)
SETQ: PI is a constant, may not be used as a variable
22. Basic Printing - print, prin1, terpri
• print always precedes its output
with a newline.
• Syntax:
(print what_to_print/expression/form)
(print "Hello World")
Output: "Hello World"
(print 100.459)
Output: 100.459
(print (+ 10 5))
(print “Hello World”)
Output:
15
"Hello World"
• prin1 just like print except that it
does not do a new line before
printing
• Syntax:
(prin1 what_to_print/expression/form)
(prin1 (+ 10 5))
(prin1 “Hello World”)
Output: "" 15"Hello World“
(+ (prin1 1) (prin1 2))
Output:
“12”
3
• terpri produces a newline. Can be called without any argument.
(prin1 (+ 10 5)) (terpri)
(prin1 "Hello World") (terpri)
(prin1 "Hello World 2")
15
"Hello World"
"Hello World 2"
23. Basic Printing – write, write-*
• write-string: It writes the characters of the specified substring of string to
the output-stream.
• write-line: It works the same way as write-string, but outputs a newline
afterwards.
• write: typically used with an object or structure
• write-*: there are other write methods like write-char and write-byte
(write-string "Hello")
(write-string “World")
‘ output Hello World
(write-line "Hello")
(write-line “World")
‘ output Hello
‘ World
(write "Hello")
‘ output Hello
24. Advanced Printing - format
• Allows string interpolation
(setq city "Bangalore")
(format t
"I like ~s as I live there. Current temp is ~s C"
city
(+ 10 20))
Output: I like "Bangalore" as I live there. Current temp is 30 C
25. Decision making (if)
• Has if condition (no else then keyword)
( if ‹condition expr›
‹if expr›
‹else or oterwise expr› )
> (if (> 2 3)
"bigger"
"smaller")
Output - "smaller"
(setq x 10)
(if (< x 10)
"x is less than 10" ;if x < 10 then returns this expression
"x is equal to or greater than 10") ;this is the else expression
Output - "x is equal to or greater than 10"
26. and
(define x 10)
(define y 20)
(if (and
(= x 10)
(= y 20))
"both conditions are true"
"false")
Output - "both conditions are true"
;next program
(define x 10)
(define y 20)
(define z 50)
(if (and
(= x 10)
(= y 20)
(= z 30))
"all conditions are true"
"all conditions are not true")
Output - "both conditions are not true"
27. or
(define x 10)
(define y 20)
(if (or
(= x 10)
(= y 10))
“one of the conditions is true"
“none of the conditions is true")
Output - "one of the condition is true"
;next program
(define x 10)
(define y 20)
(define z 50)
(if (or
(= x 15)
(= y 15)
(= z 15))
"one of the conditions is true"
"none of the conditions is true")
Output - " none of the conditions is true"
28. cond
• Allows multiple conditions
• Similar to switch case or select case
(define (check x)
(cond ((< x 0) "x is less than 0")
((= x 0) "x is equal to 0")
((> x 0) "x is greater 0")))
(check 10)
(check 0)
(check -10)
Output -
"x is greater 0"
"x is equal to 0"
"x is less than 0"
29. Data Structures - List
• List - core Lisp data structure (Lisp gets it’s name from List Processing)
Empty list: ()
> '(1 2 3 4)
(list 1 2 3 4)
> (cons 1 empty)
(1)
> (cons 1 (cons 2 empty))
(1 2)
> (list 1 2 3 4 5)
(list 1 2 3 4 5)
• A list can contain any data type
> (define lst (list 1 3 4 "Hello" 5 6 "Bangalore"))
> lst
(list 1 3 4 "Hello" 5 6 "Bangalore")
> (cons 1 (cons 2 (cons 3 (cons "Hello" (cons "World" (cons 4
empty))))))
(1 2 3 "Hello" "World" 4)
30. Data Structures - List
> (length lst) ; count the elements
7
> (list-ref lst 0) ; extract by position
1
> (list-ref lst 1)
3
> (list-ref lst 3)
"Hello"
> (list-ref lst 6)
"Bangalore"
> (reverse lst) ; reverse order
("Bangalore" 6 5 "Hello" 4 3 1)
> (append lst (list 7 8 9 "Kolkata")) ; combine lists
(1 3 4 "Hello" 5 6 "Bangalore" 7 8 9 "Kolkata")
> (member "Delhi" lst) ;check for an element
#f
> (member "Bangalore" lst)
("Bangalore")
31. List operations (car, cdr)
• car (pronounced kar) – returns the first element of the list
• cdr (pronounced kuder) – returns rest of the list without the first element.
• Alternatively called first and rest. In other languages typically called head
and tail
(define lst (list 1 3 4 "Hello" 5 6 "Bangalore"))
(car lst)
Output: 1
(cdr lst)
Output: (3 4 "Hello" 5 6 "Bangalore")
Lisp was originally implemented on the IBM 704 computer, in the late 1950s. The 704 hardware had special support
for splitting a 36-bit machine word into four parts, an "address part" and "decrement part" of 15 bits each and a
"prefix part" and "tag part" of three bits each.
- car (short for "Contents of the Address part of Register number")
- cdr ("Contents of the Decrement part of Register number")
http://en.wikipedia.org/wiki/CAR_and_CDR
1 3 4 Hello 5 6 Bangalore
car (head/first) cdr (tail/rest)
32. Moe List operations (cadr, cddr, caddr, caddr, …)
To get the second element from list – use cadr
(print (car (cdr lst)))
Alternately - (print (cadr lst))
Output: 3
All elements but first and second – use cddr
(print (cddr lst))
Output: (4 "Hello" 5 6 "Bangalore")
Third element , Forth Element -
(print (caddr lst))
Output: 4
(print (cadddr lst))
Output: "Hello"
1 3 4 Hello 5 6 Bangalore
car (head/first) cdr (tail/rest)
33. Moe List operations (cadr, cddr, caddr, caddr, …)
To get the first element from list – use first
(print (first lst)
Output: 1
To get the second element from list – use second
(print (second lst)
Output: 3
To get the third element from list – use third
(print (third lst)
Output: 4
To get the last element from list – use third
(print (last lst)
Output: “Bangalore”
1 3 4 Hello 5 6 Bangalore
first second third last
35. Associate List or alist (aka Object)
• Allows to keep key, value pairs.
• Very similar to Object
• Syntax is very similar to JavaScript object
(setf person '((first-name "Aniruddha")
(last-name "Chakrabarti")
(age 40)
(salary 123.456)))
(print person)
Output: ((FIRST-NAME "Aniruddha") (LAST-NAME "Chakrabarti") (AGE
40) (SALARY 123.456))
• Lisp provides a function, assoc, to retrieve information easily from
association lists given a retrieval key.
(print (assoc 'age person))
Output: (AGE 40)
(print (assoc 'first-name person))
Output: (FIRST-NAME "Aniruddha")
first-name Aniruddha
last-name Chakrabarti
age 40
salary 123.456
Person
36. Associate List (Cont’d)
(setf person '((first-name "Aniruddha")
(last-name "Chakrabarti")
(age 40)
(salary 123.456)))
(print person)
Output: ((FIRST-NAME "Aniruddha") (LAST-NAME "Chakrabarti") (AGE
40) (SALARY 123.456))
• rassoc, reverse assoc starts the matching from reverse direction
• copy-alist: returns a two-level deep copy of alist: it creates a new copy of
each association, so that you can alter the associations of the new alist
without changing the old one.
first-name Aniruddha
last-name Chakrabarti
age 40
salary 123.456
Person
37. Function Basics
• Functions are defined using defun statement
(defun <function name>(arg1 arg2 ... argN)
(statements))
(defun double (x) (* x 2))
(print (double 4))
8
(print (double 9.87))
19.74
(print (double "hello world"))
*** - *: "hello world" is not a number
(print (double (+ 3 5)))
16
(print (double (* 3 5)))
30
(defun printmessage (msg) (print msg))
(printmessage "Hello World")
"Hello World"
Parameter-less Function
(defun display_name ()
(print "Hello")
)
(display_name)
"Hello"
38. Function Basics
(defun add (a b)
(+ a b))
(add 10 20)
(add 10.345 20.456)
Output:
30
30.801000000000002
> (add 20 "hello")
. . +: contract violation
expected: number?
given: "hello"
argument position: 2nd
other arguments...:
20
39. Multiline Functions
(defun add (a b)
(setq res (+ a b))
(print res)
)
(add 20 30)
(add 10.345 20.456)
Output:
50
30.800999
40. Lambda Expression
• Lambda defines anonymous functions
• Function name is not declared while defining the method
((lambda (arg1 arg2 ... argN)
(statements)) value1 value2 ... valueN)
((lambda (x) (* x 2))10)
20
((lambda (x) (* x x))10)
100
((lambda (x y) (+ x y)) 10 20)
30
((lambda (x y) (* x y)) 10 20)
200
41. Structure
• Structures are one of the user-defined data type, which allows you to
combine data items of different kinds.
• Structures are used to represent a record.
• defstruct macro in LISP allows you to define an abstract record structure.
defstruct statement defines a new data type, with more than one member
for your program.
(defstruct employee
name
age
designation
salary
is-name
)
• An implicit function named make-employee is created, which is a
constructor - when invoked will create a data structure with four
components
name String
age Integer
designation String
salary Float
is-male Boolean