CSE240 – Introduction to
Programming Languages
Lecture 19:
Programming with LISP | Functions
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Function Making
• functions are created by calling a function-making macro. This macro
is called defun.
• the function returns the value of the last expression.
(defun function-name-symbol
(param1 param2 param3 ...)
expr1
expr2
expr3
...
)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Function Making
• (defun do-hello-world ( ) "Hello, World!")
DO-HELLO-WORLD
• (do-hello-world)
"Hello, World!"
• (defun add-four (x) (+ x 4))
ADD-FOUR
• (add-four 7)
11
• (defun hypotenuse (length width)
(sqrt (+ (* length length)(* width width))))
HYPOTENUSE
• (hypotenuse 7 9)
11.4017
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Function Making
• (defun first-n-chars (string n reverse-iT)
(if reverse-iT
(subseq (reverse string) 0 n)
(subseq string 0 n)))
FIRST-N-CHARS
• (first-n-chars "hello world" 5 nil)
"hello"
• (first-n-chars "hello world" 5 t)
"dlrow"
• (first-n-chars "hello world" 5 2013)
"dlrow"
Test Yourselves
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Quiz 1 | Question 1
public static double factorial (double n) {
double sum = 1;
for (double i=0;i < n; i++)
sum = sum * (1 + i);
return sum;
}
public static void main(String [] args) {
System.out.println(factorial(1000)+"");
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Quiz 1 | Answer 1
(defun factorial (n)
(let ((sum 1))
(dotimes (i n)
(setf sum (* sum (1+ i)))
)
sum
)
)
FACTORIAL
(factorial 1000)
result?
… double factorial (double n) {
double sum = 1;
for (double i=0;i < n; i++)
sum = sum * (1 + i);
return sum;
}
result?
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Quiz 1 | Question 2
public static double factorial (double n) {
if (n<=1)
return 1;
else
return n * factorial (n-1);
}
public static void main(String [] args) {
System.out.println(factorial(1000)+"");
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Quiz 1 | Answer 2
(defun factorial (n)
(if (<= n 0)
1
(* n (factorial (- n 1)))
)
)
FACTORIAL
(factorial 1000)
result?
… double factorial (double n) {
if (n<=1)
return 1;
else
return n * factorial (n-1);
}
result?
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

201801 CSE240 Lecture 19

  • 1.
    CSE240 – Introductionto Programming Languages Lecture 19: Programming with LISP | Functions Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 2 Function Making • functions are created by calling a function-making macro. This macro is called defun. • the function returns the value of the last expression. (defun function-name-symbol (param1 param2 param3 ...) expr1 expr2 expr3 ... )
  • 3.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 3 Function Making • (defun do-hello-world ( ) "Hello, World!") DO-HELLO-WORLD • (do-hello-world) "Hello, World!" • (defun add-four (x) (+ x 4)) ADD-FOUR • (add-four 7) 11 • (defun hypotenuse (length width) (sqrt (+ (* length length)(* width width)))) HYPOTENUSE • (hypotenuse 7 9) 11.4017
  • 4.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 4 Function Making • (defun first-n-chars (string n reverse-iT) (if reverse-iT (subseq (reverse string) 0 n) (subseq string 0 n))) FIRST-N-CHARS • (first-n-chars "hello world" 5 nil) "hello" • (first-n-chars "hello world" 5 t) "dlrow" • (first-n-chars "hello world" 5 2013) "dlrow"
  • 5.
  • 6.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 6 Quiz 1 | Question 1 public static double factorial (double n) { double sum = 1; for (double i=0;i < n; i++) sum = sum * (1 + i); return sum; } public static void main(String [] args) { System.out.println(factorial(1000)+""); }
  • 7.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 7 Quiz 1 | Answer 1 (defun factorial (n) (let ((sum 1)) (dotimes (i n) (setf sum (* sum (1+ i))) ) sum ) ) FACTORIAL (factorial 1000) result? … double factorial (double n) { double sum = 1; for (double i=0;i < n; i++) sum = sum * (1 + i); return sum; } result?
  • 8.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 8 Quiz 1 | Question 2 public static double factorial (double n) { if (n<=1) return 1; else return n * factorial (n-1); } public static void main(String [] args) { System.out.println(factorial(1000)+""); }
  • 9.
    Javier Gonzalez-Sanchez |CSE 240 | Fall 2017 | 9 Quiz 1 | Answer 2 (defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1))) ) ) FACTORIAL (factorial 1000) result? … double factorial (double n) { if (n<=1) return 1; else return n * factorial (n-1); } result?
  • 10.
    CSE240 – Introductionto Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.