Functional programming -- 2008-2009 -- info.uvt.ro/Laboratory 1
Appearance
Optional (but highly recommended) reading
[edit]The following essays are stolen from Shriram Krishnamurthi:
- Beating the Averages, by Paul Graham;
- Teach Yourself Programming in Ten Years, by Peter Norvig;
- The Perils of JavaSchools, by Joel Spolsky;
References
[edit]From the book Practical Common Lisp:
- (m) Chapter 1., Introduction: Why Lisp? (the entire chapter);
- (m) Chapter 2., Lather, Rinse, Repeat: A Tour of the REPL (all sections from the Free Your Mind: Interactive Programming section, up to, and including, the "Hello, World," Lisp Style section; optionally the entire chapter);
- (m) Chapter 4., Syntax and Semantics (all sections from the beginning up to, but excluding, the Macros section, but also read the Truth, Falsehood, and Equality section);
Overview
[edit]- Concepts
- programming paradigms:
- functional programming specific concepts:
- Languages
- pure functional:
- mainly functional:
- supporting functional style:
- Python;
- Ruby;
- JavaScript;
- Lua;
Tools
[edit]- Common Lisp implementation;
- GNU CLISP -- official site;
- GNU CLISP Win32 package;
- SSH client for remote access to the university's server
goliat.info.uvt.ro
; - PuTTY -- official site;
- PuTTY Win32 package;
- Lisp aware text editor for Windows;
- SciTE -- official site;
Basic expressions
[edit]- Numbers
3.14 20 1/3 55.2e-20
- Symbols
abc function-1 min-max 1+ 2pi << >> string->
- Strings
"abc" "123CDE"
- Lists
(1 2) (1 (2 3) (4 (5 6)) 7) (1 abc 2 cde) ("abc" 1 (33 "4")) () (1 () a) (() ())
- Comments
; this is a comment (print "hello") ; this in another comment
Basic operations
[edit]- Arithmetic expressions
+
,-
,*
,/
,mod
,rem
:
(+ 1 2 3 4) ; => 10 (- 1 2 3 4) ; => -9 (* 1 2 3 4) ; => 24 (/ 1 2 3 4) ; => 1/24 (mod 7 3) ; => 1 (rem 7 3) ; => 1
1+
,1-
:
(1+ 2) ; => 3 (1- 2) ; => 1
min
,max
:
(min 1 2 3) ; => 1 (max 1/2 2 33/5) ; => 33/5
abs
,floor
,ceiling
,trucncate
,round
:
(abs -2.3) ; => 2.3 (floor 1/2) ; => 1 (truncate 1/2) ; => 1 (ceiling 1/2) ; => 2 (round 1.5) ; => 2 (round 2.5) ; => 2
sqrt
,exp
,expt
,log
:
(sqrt 1/4) ; => 1/2 (exp 2) ; => 7.389056 (expt 5/3 2) ; => 25/9 (log (exp 2)) ; => 2
signum
:
(signum -55) ; => -1 (signum -55/3) ; => -1 (signum -55.0) ; => -1.0
sin
,cos
,tan
:
(sin (/ pi 2)) ; => 0.707106...
- Arithmetic predicates
zerop
,plusp
,minusp
:
(zerop 0) ; => t (zerop 1) ; => nil (plusp 1) ; => t (plusp -1) ; => nil (plusp 0) ; => nil (minusp -1) ; => t (minusp 1) ; => nil (minusp 0) ; => nil
<
,<=
,=
,>=
,>
,/=
:
(< 1 2 3) ; => t (< 1 3 1) ; => nil (= 1 2 3) ; => nil (/= 1 2 3) ; => t
evenp
,oddp
:
(evenp 1) ; => nil (oddp 1) ; => t
- Boolean expressions
not
,null
:
(not nil) ; => t (not ()) ; => t (not t) ; => nil (not 1) ; => nil (null nil) ; => t (null ()) ; => t (null '(1 2 3)) ; => nil (null 1) ; => nil
and
,or
:
(and t nil) ; => nil (and t t) ; => t (and 1 2 3) ; => 3 (and 1 nil) ; => nil (or t nil) ; => t (or nil nil) ; => nil (or 1 2 nil) ; => 1 (or nil 1 nil) ; => 1
- Type predicates
atomp
:
(atom 'a) ; => t (atom "a") ; => t (atom 1) ; => t (atom nil) ; => t (atom '(1 2)) ; => nil (atom ()) ; => t
listp
:
(listp ()) ; => t (listp '(1 2)) ; => t (listp t) ; => nil (listp nil) ; => t
symbolp
:
(symbolp t) ; => t (symbolp nil) ; => t (symbolp "a") ; => nil (symbolp 'a) ; => t (symbolp ()) ; => nil
numberp
,integerp
,realp
,rationalp
,floatp
,complexp
;
- Equality predicates
eq
,eql
,equal
,equalp
(eq 1 1) ; => t (eq 'a 'a) ; => t (eq 1.0 1.0) ; => nil (eq "a" "a") ; => nil (eq 1 1.0) ; => nil (eq "a" "A") ; => nil (eq "a" 'a) ; => nil (eql 1.0 1.0) ; => t (eql "a" "a") ; => nil (eql 1 1.0) ; => nil (eql "a" "A") ; => nil (eql "a" 'a) ; => nil (equal 1.0 1.0) ; => t (equal "a" "a") ; => t (equal 1 1.0) ; => nil (equal "a" "A") ; => nil (equal "a" 'a) ; => nil (equalp 1.0 1.0) ; => t (equalp "a" "a") ; => t (equalp 1 1.0) ; => t (equalp "a" "A") ; => t (equalp "a" 'a) ; => nil
The current page is a simplified view of the page Laboratory 1 from the previous years, which in turn is based on Laboratory 1 by Cornel Izbasa;