Review of Functional Programming

Similar documents
Lisp Basic Example Test Questions

Functions, Conditionals & Predicates

Imperative, OO and Functional Languages A C program is

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Recursion & Iteration

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lecture Notes on Lisp A Brief Introduction

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

FP Foundations, Scheme

Common LISP-Introduction

Common Lisp. Blake McBride

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Functional programming with Common Lisp

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Introduction to Scheme

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional Programming. Pure Functional Languages

A Quick Introduction to Common Lisp

A Genetic Algorithm Implementation

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Lisp. Versions of LISP

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

1.3. Conditional expressions To express case distinctions like

Midterm Examination (Sample Solutions), Cmput 325

Functional Programming. Big Picture. Design of Programming Languages

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Sample Final Exam Questions

Functional Programming. Pure Functional Languages

Scheme Quick Reference

Lambda Calculus see notes on Lambda Calculus

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Introduction to Functional Programming and basic Lisp

Functional Programming. Pure Functional Programming

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Scheme Quick Reference

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Announcements. Today s Menu

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

ALISP interpreter in Awk

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Documentation for LISP in BASIC

CS61A Midterm 2 Review (v1.1)

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Functional Programming

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Building a system for symbolic differentiation

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

A Brief Introduction to Scheme (II)

Functional Programming with Common Lisp

Common LISP Tutorial 1 (Basic)

User-defined Functions. Conditional Expressions in Scheme

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Programming Languages

4/19/2018. Chapter 11 :: Functional Languages

Project 2: Scheme Interpreter

Lambda Calculus. Gunnar Gotshalks LC-1

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

CS 314 Principles of Programming Languages. Lecture 16

CSCI337 Organisation of Programming Languages LISP

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction to lambda calculus Part 3

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

John McCarthy IBM 704

Imperative languages

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

A little bit of Lisp

Lambda Calculus LC-1

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Department of Computer and information Science Norwegian University of Science and Technology

15 Unification and Embedded Languages in Lisp

Programming Languages

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Building a system for symbolic differentiation

An Explicit-Continuation Metacircular Evaluator

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Functional Languages. Hwansoo Han

INF4820. Common Lisp: Closures and Macros

Intro. Scheme Basics. scm> 5 5. scm>

CS 314 Principles of Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

LECTURE 16. Functional Programming

CS A331 Programming Language Concepts

Heap storage. Dynamic allocation and stacks are generally incompatible.

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CS 314 Principles of Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2005

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

SOFTWARE ARCHITECTURE 6. LISP

Transcription:

Review of Functional Programming York University Department of Computer Science and Engineering 1

Function and # It is good programming practice to use function instead of quote when quoting functions. And it can be abbreviated as # instead of Example: Instead of: > (mapcar sqrt (1 4 9 16)) (1 2 3 4) Use: > (mapcar # sqrt (1 4 9 16)) (1 2 3 4) York University CSE 3401 V. Movahedi 17_MoreOnFunctions 2

Assume we have entered the following expressions in the LISP interpreter: > (setq lst '(1 2 3 4)) (1 2 3 4) > (setq fname #'(lambda (x) (* 10 x))) #<FUNCTION :LAMBDA (X) (* 10 X)> How would LISP respond to the following? > (car lst) 1 > (cdr lst) (2 3 4) > (cadr lst) 2 > (fname lst) Error - EVAL: undefined function FNAME 3

> (setq lst '(1 2 3 4)) (1 2 3 4) > (setq fname #'(lambda (x) (* 10 x))) #<FUNCTION :LAMBDA (X) (* 10 X)> > (apply fname lst) Error- EVAL/APPLY: too many arguments given to :LAMBDA > (apply fname (car lst)) Error- EVAL/APPLY: too few arguments given to :LAMBDA > (apply fname (list (car lst))) 10 > (mapcar fname lst) (10 20 30 40) > (mapc fname lst) (1 2 3 4) 4

> (setq x 5) 5 > (setq lst '(1 2 3 4)) (1 2 3 4) > (cons x nil) (5) > (5. nil) Error- EVAL: 5 is not a function name > '(5. nil) (5) > (list 5 nil) (5 nil) > (append 5 nil) Error- APPEND: 5 is not a list > (append (5) nil) Error- EVAL: 5 is not a function > (append '(5) nil) (5) 5

> (setq x 5) 5 > (setq lst '(1 2 3 4)) (1 2 3 4) > (setq gname #'(lambda (x) (cons x 'x))) #<FUNCTION :LAMBDA (X) (CONS X 'X)> > (cons x 'x) (5. X) > (cons '(1 2 3) 'x) ((1 2 3). X) > (mapcar gname lst) ((1. X) (2. X) (3. X) (4. X)) > (maplist gname lst) (((1 2 3 4). X) ((2 3 4). X) ((3 4). X) ((4). X)) 6

Use cond to write a function f1 as follows: -1 x < 0 f1(x)= 1 0 <= x < 10 2 10 <= x < 30 3 x >= 30 (defun f1 (x) (cond ((< x 0) -1) ((< x 10) 1) ((< x 30) 2) (t 3) ) ) 7

Use cond to write a function f2 with two arguments x and lst that does the following: - If x is zero, it returns true - If x is a positive number, it returns the first two elements of lst - If x is a negative number, it opens the file "data.txt", reads from it once and returns the read number (we'll assume it will be a number) as string containing the number as a float with 2 digits after the decimal point. - If x is anything else, it returns nil 8

(defun f2a (x lst) (cond ((and (numberp x) (zerop x)) T) ((and (numberp x) (> x 0)) (list (car lst) (cadr lst))) ((and (numberp x)(< x 0)) (setq ins (open "data.txt" :direction :input)) (format nil "~5,2f" (read ins)) (close ins)) (t nil))) > (f2a 'a '(1 2 3)) NIL > (f2a 0 '(1 2 3)) T > (f2a 1 '(1 2 3)) (1 2) > (f2a -1 '(1 2 3)) Not needed Not needed Not working if length<2 T Does not work properly! Reason: returns the return value of (close ins) 9

(defun f2b (x lst) (cond ((and (numberp x) (zerop x))) ((and (numberp x) (> x 0)) (list (car lst) (cadr lst))) ((numberp x) (setq ins (open "data.txt" :direction :input)) (setq y (read ins)) (close ins) (format nil "~5,2f" y) ))) > (f2b -2 '(1 2 3)) "10.00" > y 10 global variable 10

Improve the code to not influence any global variables - use aux variables (defun f2c (x lst &aux ins y) (cond ((and (numberp x) (zerop x))) ((and (numberp x) (> x 0)) (list (car lst) (cadr lst))) ((numberp x) (setq ins (open "data.txt" :direction :input)) (setq y (read ins)) (close ins) (format nil "~5,2f" y) ))) > (setq y 3) 3 > (f2c -2 '(1 2 3)) "10.00" > y 3 11

Improve the code to not influence any global variables - use let (defun f2c (x lst) (let (ins y) (cond ((and (numberp x) (zerop x))) ((and (numberp x) (> x 0)) (list (car lst) (cadr lst))) ((numberp x) (setq ins (open "data.txt" :direction :input)) (setq y (read ins)) (close ins) (format nil "~5,2f" y) )))) 12

Improve the code to not influence any global variables - use lambda (defun f2c (x lst) ((lambda (ins y) (cond ((and (numberp x) (zerop x))) ((and (numberp x) (> x 0)) (list (car lst) (cadr lst))) ((numberp x) (setq ins (open "data.txt" :direction :input)) (setq y (read ins)) (close ins) (format nil "~5,2f" y) ))) nil nil)) 13

Improve the code to return a maximum of two elements if x is positive (not to crash if length(lst)<2) (defun f2c (x lst &aux ins y) (cond ((and (numberp x) (zerop x))) ((and (numberp x) (> x 0)) (do ((tlst lst (cdr tlst)) (n 2 (1- n)) (rslt nil (append rslt (list (car tlst)))) ) ((or (null tlst) (zerop n)) rslt))) ((numberp x) ) > (f2c 10 '(1 2)) (1 2) > (f2c 10 '(1)) (1) > (f2c 10 '()) () 14

If f3 is defined as follows, how would LISP respond to the following? (defun f3 (lst n p) (do ((tlst lst (cdr tlst)) (rslt '(0. nil) (cons (car tlst) rslt)) (i (1- n) (1- i))) ((zerop i) (cond ((zerop p) rslt) (t n))) (if (null tlst) (return "Error")))) > (f3 '(1 2 3) 3 0) (2 1 0) > (f3 '(1 2 3) 3 1) 3 > (f3 '(1 2 3) 5 1) "Error" > (f3 '(1 2 3) 5 0) "Error" > (f3 '(1 2 3) 4 0) (3 2 1 0) 15

What if we have a do* instead of do? (defun f3b (lst n p) (do* ((tlst lst (cdr tlst)) (rslt '(0. nil) (cons (car tlst) rslt)) (i (1- n) (1- i))) ((zerop i) (cond ((zerop p) rslt) (t n))) (if (null tlst) (return "Error")))) > (f3b '(1 2 3) 3 0) (3 2 0) > (f3b '(1 2 3) 3 1) 3 > (f3b '(1 2 3) 5 1) "Error" > (f3b '(1 2 3) 5 0) "Error" > (f3b '(1 2 3) 4 0) (NIL 3 2 0) 16

Alonzo Church has defined the natural numbers in lambda calculus (known as the Church numerals) as follows: 0 := λfx.x 1 := λfx.f x 2 := λfx.f (f x) 3 := λfx.f (f (f x)) Show that if PLUS is defined as PLUS := λmnfx.m f (n f x) then adding (PLUS) two and one is equivalent to three. 17

PLUS 2 1 = (λmnfx.m f (n f x)) 2 1 β ((λnfx.m f (n f x))[m := 2]) 1 = (λnfx.2 f (n f x)) 1 = (λnfx.(λfx.f (f x)) f (n f x)) 1 β (λnfx.((λx.f (f x)) [f:=f]) (n f x)) 1 = (λnfx.(λx.f (f x)) (n f x)) 1 β (λnfx.(f (f x))[x:= (n f x)]) 1 = (λnfx.f (f (n f x))) 1 β (λfx.f (f (n f x))) [n := 1] = λfx.f (f (1 f x)) = λfx.f (f ((λfx.f x) f x)) β λfx.f (f ( ((λx.f x)[f:=f]) x)) = λfx.f (f ((λx.f x) x)) β λfx.f (f ((f x) [x:=x])) = λfx.f (f (f x)) which is equivalent to 3 18

Write a function that creates a sequence of bits (0 or 1) of length len: (defun rndx (len) (do ( (i len (1- i)) (x nil (cons (random 2) x))) ((zerop i) x))) 19

Convert a sequence of bits to its decimal equivalent: (defun reverse (lst) (do ( (x lst (cdr x)) (result nil (cons (car x) result))) ((null x) result))) (defun x2dec (x) (do ((tx (reverse x) (cdr tx)) (exp 0 (1+ exp)) (dec 0 (+ dec (* (car tx) (expt 2 exp))))) ((null tx) dec))) 20

Write a function that finds the length of a sequence and use that to re-write the previous function w/o reversing: (defun getlength (lst) (do ((tlst lst (cdr tlst)) (n 0 (1+ n))) ((atom tlst) n))) (defun x2dec2 (x) (do ((tx x (cdr tx)) (exp (1- (getlength x)) (1- exp)) (dec 0 (+ dec (* (car tx) (expt 2 exp))))) ((null tx) dec))) 21

Write a function that inverts a random bit in a sequence with a given probability. (defun invert@loc (lst loc) (if (zerop loc) (cons (mod (1+ (car lst)) 2) (cdr lst)) (cons (car lst) (invert@loc (cdr lst) (1- loc))))) (defun mutation (x pm) (if (< (random 1.0) pm) (invert@loc x (random (getlength x))) x )) 22

[ref: CSE3401 Summer 2009 Assignment #2] Write a recursive function COMPRESS and DECOMPRESS that takes a list as a parameter and replaces any consecutive occurrence of elements with the element and its count. For example: > (compress (a a a b b x 2 2)) (a 3 b 2 x 1 2 2) > (decompress (a 3 b 2 x 1 2 2)) (a a a b b x 2 2) 23

> (compress (a a a b b x 2 2)) (a 3 b 2 x 1 2 2) (defun readsame (plst) (do ((tlst plst (cdr tlst)) (n 0 (1+ n))) ((or (null tlst) (not (equal (car plst) (car tlst)))) (return (list (list (car plst) n) tlst)) ))) (defun compress (lst) (cond ((null lst) nil) (t (let ((rec (readsame lst))) (append (car rec) (compress (cadr rec))))) )) 24

> (decompress (a 3 b 2 x 1 2 2)) (a a a b b x 2 2) (defun writesame (a n) (do ((rslt nil (cons a rslt)) (i n (1- i))) ((zerop i) rslt))) (defun decompress (lst) (cond ((null lst) nil) (t (append (writesame (car lst) (cadr lst)) (decompress (cddr lst)))) )) 25