Lisp EN -- Laboratory 1 -- 2006-2007 -- info.uvt.ro

From Wikiversity
Jump to: navigation, search
Important! These pages are somehow outdated and it is recommended to consult the newer version at Functional programming -- 2008-2009 -- info.uvt.ro (by Ciprian Crăciun).

Lisp EN -- 2006-2007 -- info.uvt.ro


Introduction[edit]

Programming paradigms[edit]


Concepts[edit]


Syntax[edit]

  • Atoms
    • Numbers
3.14
20
1/3
55.2e-20
    • Symbols
a
abc
1+
2pi
    • Special symbols
      • t
      • nil
    • Lists
(1 2)
(1 (2 3) (4 (5 6)) 7)
      • Special lists
        • nil
        • ()
  • Strings
"abc"
"123CDE"
  • Symbolic expressions (S-expressions)
("abc" 1 (33 "4"))
  • Comments
; this is a comment
(print 1) ; this in another comment


Arithmetic expressions[edit]

Examples[edit]

  • Comparisons with other programming languages or mathematical notation
    • 1 + 2 + 3 * 4 => (+ 1 2 (* 3 4))
    • sin(30) => (sin 30)
    • (((1 / 2) / 3) / 4) => (/ 1 2 3 4)


Number types[edit]

  • integer (extends number)
1
100
123456
  • real (extends number)
    • rational (extends real)
1/2
2/3
    • float (extends real)
1.0
20.33e01
3e-14
  • complex


Constants[edit]

  • pi


Arithmetic functions[edit]

  • + -* /:
(+ 1 2 3 4) => 10
(- 1 2 3 4) => -8
(* 1 2 3 4) => 24
(/ 1 2 3 4) => 1/24
  • mod and rem
(mod 7 3) => 1
(rem 7 3) => 1
  • 1+ and 1-
(1+ 2) => 3
(1- 2) => 1
  • min and max
(min 1 2 3) => 1
(max 1/2 2 33/5) => 33/5
  • abs
(abs -2.3) => 2.3
  • floor, ceiling, truncate and round
(floor 1/2) => 1
(truncate 1/2) => 1
(ceiling 1/2) => 2
(round 1.5) => 2
(round 2.5) => 2
  • sin, cos and tan
(sin (/ pi 2)) => 0.707106...
  • signum
(signum -55) => -1
(signum -55/3) => -1
(signum -55.0) => -1.0
  • sqrt, exp, expt and log
(sqrt 1/4) => 1/2
(exp 2) => 7.389056
(expt 5/3 2) => 25/9
(log (exp 2)) => 2

Boolean expression[edit]

Questions[edit]

  • What is considered true: t and anything except nil.
  • Does () represent true or false?


Predicates[edit]

Objec type or identity predicates[edit]

  • atom
(atom 'a) => t
(atom "a") => t
(atom 1) => t
(atom nil) => t
(atom '(1 2)) => nil
(atom ()) => ???
  • listp
(listp ()) => t
(listp '(1 2)) => t
(listp t) => nil
(listp nil) => ???
  • symbolp
(symbolp t) => t
(symbolp nil) => t
(symbolp "a") => nil
(symbolp 'a) => t
(symbolp ()) => ???
  • null
(null t) => nil
(null 'a) => nil
(null nil) => t
(null '(1 2 3)) => nil
(null ()) => ???

Arithmetic predicates[edit]

  • numberp
  • integerp
  • realp
  • rationalp
  • floatp
  • complexp
  • minusp and plusp
(minusp 1) => nil
(plusp 1) => t
(minusp 0) => nil
(plusp 0) => nil
  • zerop
(zerop 1) => nil
  • <, <=, =, >=, >, and /=
(< 1 2 3) => t
(< 1 3 1) => nil
(= 1 2 3) => nil
(/= 1 2 3) => t
  • evenp and oddp
(evenp 1) => nil
(oddp 1) => t

Boolean predicates[edit]

  • not and null
  • and
(and t nil) => nil
(and t t) => t
(and 1 2 3) => 3
(and 1 nil) => nil
  • or
(or t nil) => t
(or nil nil) => nil
(or 1 2 nil) => 1
(or nil 1 nil) => 1


Equality predicates[edit]

  • eq -- verifies identity
(eq 1 1) => t
(eq 1 1.0) => nil
(eq "a" "a") => nil
  • eql -- verifies value
(eql 1 1.0) => nil
(eql '(1 2) '(1 2)) => nil
(eql "a" "a") => nil
  • equal -- verifies representation
(equal 1 1.0) => nil
(equal "a" "a") => t
(equal "a" "A") => nil
(equal '(1 2) '(1 2)) => t
  • equalp -- verifies representation ignoring case
(equalp 1 1.0) => t
(equalp "a" "A") => t


Lists[edit]

Examples[edit]

(1 2 3 4)
(a b c d)
(1 a b 2 c 3)
((1) (2) (()))


Quoting[edit]

  • quote and '
(+ 1 2) => 3
'(+ 1 2) => (+ 1 2)


Functions[edit]

  • length
(length ()) => 0
(length nil) => ???
(length '(1 2 3)) => 3
(length '(1 (2 3 4 5) 6)) => 3
  • car
(car ()) => nil
(car '(1 2)) => 1
  • cdr
(cdr ()) => nil
(cdr '(1 2 3)) => (2 3)
  • Using car and cdr together
(car (cdr '(1 2 3))) => 2
(cadr '(1 2 3)) => 2
(cddr '(1 2 3)) => (3)
(caddr '(1 2 3)) => 3
  • first, second, ..., tenth
(sixth '(1 2 3 4 5 6 7)) => 6
  • nth and nthcdr
(nth 3 '(1 2 3 4 5 6)) => 4
(nthcdr 3 '(1 2 3 4 5 6)) => (4 5 6)
  • list
(list 'a 'b 'c 'd) => (a b c d)
(list '(1 2) '(3 4)) => ((1 2) (3 4))
  • append
(append '(1 2) '(3 4)) => (1 2 3 4)
(append 1 2 3) => error!
  • reverse
(reverse '(1 2 3 4)) => (4 3 2 1)
(reverse '(1 (2 3) 4)) => (4 (2 3) 1)
  • member
(member 5 '(1 2 3)) => nil
(member 2 '(1 2 3)) => (2 3)
(member 2 '(1 (2 3) 4)) => nil
  • subst
(subst 'a 1 '(1 2 1 3)) => (a 2 a 3)
(subst 'a 1 '(1 (2 1) 3)) => (a (2 a) 3)


User defined functions[edit]

  • defun
(defun <function-name> (<argument-1> ...) <statement-1> ...)
(defun my-sum (a b) (+ a b))
(my-sum 1 2) => 3
(defun my-evenp (n) (zerop (mod n 2)))
(my-evenp 2) => t

Assignment 1[edit]

Until next Wednesday (2007-03-07) 24:00 you will have to send me by email the following assignment.

When you send the email please include in the subject "[LISP-EN-2] First_name Last_name" for the second year and "[LISP-EN-3] First_name Last_name" for the third year. Please paste the code inside the email, DO NOT ATTACH any files.

Write 10 user defined functions (by using defun), with a minimum of 2 parameters (one parameter is accepted but only as an exceptional case), which uses some of the library functions described in this lab. Please also include a short description for each one and 2 or 3 test cases with the results.

For example:

(defun pol2 (x a b c) (+ (* a x x) (* b x) c))
(pol2 1 2 3 4) => 9
(defun my-< (a b) (minusp (- a b)))
(my-< 1 2) => t

Ciprian Dorin Craciun

2007-03-01

ccraciun@info.uvt.ro