Lisp Basic Example Test Questions

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

Functional Programming. Pure Functional Programming

Lecture Notes on Lisp A Brief Introduction

Functional programming with Common Lisp

Review of Functional Programming

Announcements. Today s Menu

Midterm Examination (Sample Solutions), Cmput 325

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional Languages

Lambda Calculus see notes on Lambda Calculus

Common LISP-Introduction

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

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

Functional Programming. Pure Functional Languages

User-defined Functions. Conditional Expressions in Scheme

Imperative languages

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

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

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

Project 2: Scheme Interpreter

CS61A Midterm 2 Review (v1.1)

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

CSCI337 Organisation of Programming Languages LISP

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

ALISP interpreter in Awk

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

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Imperative, OO and Functional Languages A C program is

Functional Programming. Contents

Common Lisp. Blake McBride

UMBC CMSC 331 Final Exam

Lisp. Versions of LISP

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

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

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

Functions, Conditionals & Predicates

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

by the evening of Tuesday, Feb 6

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))

Recursion & Iteration

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

FP Foundations, Scheme

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Building a system for symbolic differentiation

UMBC CMSC 331 Final Exam

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

Documentation for LISP in BASIC

Functional Programming. Big Picture. Design of Programming Languages

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

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

A Brief Introduction to Scheme (II)

A little bit of Lisp

Problem Set 4: Streams and Lazy Evaluation

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

MIDTERM EXAMINATION - CS130 - Spring 2005

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

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

Principles of Programming Languages 2017W, Functional Programming

Building a system for symbolic differentiation

Common LISP Tutorial 1 (Basic)

CSE413 Midterm. Question Max Points Total 100

19 Machine Learning in Lisp


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

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

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

CS 842 Ben Cassell University of Waterloo

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

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

A Genetic Algorithm Implementation

Streams and Lazy Evaluation in Lisp

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

15 Unification and Embedded Languages in Lisp

A Quick Introduction to Common Lisp

Functional Programming - 2. Higher Order Functions

CMSC 331 Final Exam Section 0201 December 18, 2000

INF4820. Common Lisp: Closures and Macros

Chapter 15. Functional Programming Languages

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Introduction to Functional Programming

Sample Final Exam Questions

Allegro CL Certification Program

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Introduction to Functional Programming and basic Lisp

1.3. Conditional expressions To express case distinctions like

Lambda Calculus. Gunnar Gotshalks LC-1

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Heap storage. Dynamic allocation and stacks are generally incompatible.

CSc 520 Principles of Programming Languages

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

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

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Associative Database Managment WIlensky Chapter 22

Associative Database Managment

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

April 2 to April 4, 2018

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Transcription:

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2 3) ) ( setq c d ) ( setq b e ) ( set c b ) ( setq f ( ( a ) b ) ) ( defun b ( x ) ( mapcar # list x ) ) ( setq e ( ( lambda ( x ) x ) a ) ) What will the following forms evaluate to? State error if the form does not evaluate correctly and explain why? 1. ( a a ) 2. ( funcall a (list c b d)) 3. ( apply a (a b c)). ( eval a (a b c) ) 5. ( eval e ) 6. ( b f ) 7. (mapcar b ( (1 2 3) (2 3) (3) ) ) 2. 3. Write a recursive function interleave (list-1 list-2) that returns a list in which the elements of list-1 are interleaved with list-2. The following are examples. Use only the Lisp functions from the list: cond, list, append, cons, car, cdr, +, atom, numberp, null. (interleave (a (b b) c) (1 2 3 5)) (a 1 (b b) 2 c 3 5) (interleave (1 2 3 5) (a (b b) c)) (1 a 2 (b b) 3 c 5) Assume the following forms have been typed into the interpreter and evaluated in the given order. ( defun a ( y ) ( reverse y ) ) ( setq a (x y z) ) ( defun b ( x ) ( mapcar # list x ) ) ( setq b e ) ( setq c d ) ( set c b ) ( setq e # (lambda ( x ) x ) ) What will the following forms evaluate to? If the form does not evaluate correctly, state error and give a reason why? 1. ( a a )

2009 November 30 Lisp Basic Example Test Questions Page 2 of 7 2. ( funcall a (list a c b)) 3. ( apply a (a b c)). ( eval c) 5. ( funcall (eval b) a ) 6. (mapcar b (maplist e a ) ). 5. 6. Write and document a recursive Lisp definition for the mapcar function that can have an arbitrary number of input lists. The mapcar functions stops as soon as any one of the input lists becomes empty. Do not use global variables. Do not use let, prog and similar features to introduce local variables; use only parameters to functions as local variables. You may use support functions. Define a recursive Lisp function, simple, which takes one argument, a list of numbers and atoms and returns a list containing two items. The first is the sum of the numbers and the second is the number of non-number atoms. For example: (simple (a 2 3 b d 1 c ) ) (10 ) (simple (a b () ) ) (0 3) For full marks, use only one function,; do not use a helper function. Use only the Lisp functions cond, list, append, cons, car, cdr, +, atom, numberp, null. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( list y y ) ) ( setq a c ) ( defun b ( x ) ( maplist # list x ) ) ( setq c d ) ( setq e ( ( lambda ( x ) x ) a ) ) ( setq b e ) ( set c b ) ( setq f ( ( a ) b ) ) What will the following forms evaluate to? 1. ( a a ) 2. ( funcall e c) 3. ( cons a b ). ( eval d ) 5. ( eval ( eval (eval (eval (quote e ) ) ) ) ) 6. 7. (mapcar b ( ( 1 ) ( 2 ) ( 3 ) ) ) 7. 8. Write a recursive function, (defun index (???)???) such that a call of the form (index array indexlist) returns the array element array[i1,i2,...,in], where indexlist = (I1,I2,...,In). Assume no errors; i.e. no out of bound indices. There is no fixed size for the number of dimensions. Use the function pth from part B. Do not use length, last, butlast, etc., use first (or car) and rest (or cdr). Write a your own recursive version mymaplist of the maplist function. If possible, do not define additional functions but it is better to have them with a correct and commented function than have an

2009 November 30 Lisp Basic Example Test Questions Page 3 of 7 9. 10. 11. incorrect function. Hints: Recall the functions some and every. Maplist terminates when one of the input lists becomes nil. B Write a recursive function, (defun nth (pos list)???), that returns the n'th item from a list. Assume the list has at least n items. (nth 1 alist) is to return the first item in alist. Write a recursive function, (defun diagof(thematrix)...) to return the diagonal of a square matrix. Assume the input is error free. You may write support functions. Do not use global variables. Do not use let, prog and similar features to introduce local variables; use only parameters to functions as local variables. A What is the result of evaluating the following Lisp expression? Explain your answer (setq a (cons a a)) 12. B Given the following function definition. (defun oh (condition thenphrase elsephrase) (cond (condition thenphrase) (t elsephrase))) Explain the following >(setq a ) >(oh (< 5) (setq a (1+ a)) (setq a (1- a))) 5 >a >(oh (> 5) (setq a (1+ a)) (setq a (1- a))) >a 13. C Suppose the following function is defined (defun test (x) (reverse x)). Will the expression (apply test (a b c)) execute correctly. If yes, explain why. If not, explain why and modify the expression so it works. C Suppose the following function is defined (defun test (x) (reverse x)). Will the expression (funcall test (list a b c)) execute correctly. If yes, explain why. If not, explain why and modify the expression so it works. (eval (test (a b c))) The following Lisp function is defined where the function reverse reverses the list x. (defun test (x) (reverse x)) Will the following expressions execute correctly. If yes, explain why. If not, explain why not and modify the expression so it works. (apply test (a b c)) (funcall test (list a b c)) (eval test (a b c))

2009 November 30 Lisp Basic Example Test Questions Page of 7 1. 15. 16. A 17. 18. (eval (test (a b c))) Write a recursive Lisp function flatten(alist) to return all the atoms, except nil, at all levels in alist as a single level list while retaining their order. The following are examples. (flatten (A (B (C D) E) (F G))) (A B C D E F G) (flatten (1 () 2)) (1 2) (flatten (1 () (()) ( 2 () 3))) (1 2 3) For this question, both parts A and B, you may use only the following Lisp functions: append, atom, car, cdr, cond, cons, defun, list, null, +, and =. You may use combinations of car and cdr such as caddr. Lower-atoms was called deeper-atoms in F01 and F0 A Write a recursive program, lower-atoms, that embeds each atom, except nil, in a list containing only the atom. Use only the basic Lisp operators: car, cdr, cons, cond, null and atom. Examples: (a) ((a)) (()) (()) (a () b) ((a) () (b)) (a b (c (b d)) (e)) ((a) (b) ((c) ((b) (d))) ((e))) B Write a recursive program, lift-atoms, that lifts each atom, except nil, that is in a list containing only the atom up one level. This is the inverse of part A. Use only the basic Lisp operators: car, cdr, cons, cond, null and atom. Examples: ((a)) (a) (()) (()) ((a) () (b)) (a () b) ((a) (b) ((c) ((b) (d))) ((e))) (a b (c (b d)) (e)) You may write support functions, although you get lower evaluation if you do. Do not use global variables. Do not use let, prog and similar features to introduce local variables; use only parameters to functions as local variables. Write a recursive function, (defun myinter (set1 set2)... ), that computes the set intersection of set1 and set2. Use the member function (member item list), it returns the sublist beginning at the item, if item is in the list and returns nil otherwise. Write a recursive function, remove-nth that removes the n th item from every list at all levels. Counting begins at 1. You cannot use any implicitly recursive function, such as mapcar, length, etc., except for append. You must use the following definitions of prefix and suffix in your definition. (prefix 3 (10 20 30 0 50)) (10 20 30) ;; First 3 items. (suffix 3 (10 20 30 0 50)) (30 0 50) ;; Last 3 items. Use car, cdr and cons for the basic list operations, and cond for conditional statements. You are permitted to and will need to write recursive support functions. Precondition: n 1. (defun remove-nth (n list) ;; You supply the rest Write a recursive function, remove-nth that removes the n th element from every list at all levels. Counting begins at 1. You cannot use any implicitly recursive function, such as mapcar, length, etc., except for append, and the following functions prefix and suffix. You must use prefix and suffix in your definition. (prefix 3 (10 20 30 0 50)) (10 20 30) (suffix 3 (10 20 30 0 50)) (30 0 50) Next time define 3 as the position not length.

2009 November 30 Lisp Basic Example Test Questions Page 5 of 7 19. 20. 21. Use car, cdr and cons for the basic list operations, and cond for conditional statements. You are permitted to and will need to write recursive support functions. Precondition: n 1 (defun remove-nth (n list) ;; You supply the rest Write a recursive function, insert-nth that inserts item as the n th element into every list at all levels. Counting begins at 1. You cannot use any implicitly recursive function, such as mapcar, length, etc. Use car, cdr and cons for the basic list operations. You are permitted to and will need to write recursive support functions. Precondition: n 1 and n 1+length(shortest list at any level in list) (defun insert-nth (item n list) ;; You supply the rest Write a recursive function, removecontig(thelist) that reduces all instances of a contiguous sequence of identical items to one instance at all levels of thelist. You cannot use any implicitly recursive function, such as mapcar, length, etc. Use cond, car, cdr and cons for the basic list operations. You are permitted to write recursive support functions. Precondition: thelist is a list (defun removecontig (thelist) ;; You supply the rest Assume the following forms have been typed into the Lisp interpreter and evaluated. ( defun a ( x ) ( values (list x) x ) ) ( setq a '( a b ) ) ( defun b ( x ) `( x,x ) ) (setq b ( cdr a ) ) ( setq c ( car a ) ) ( setq d c ) ( setq e ( (lambda ( x ) ( list x ) ) d ) ) 22. What will the following forms evaluate to? 1. ( cons c ( car a ) ) 2. ( cons e b ) 3. ( eval a ). ( let (( a b ) ( y a )) ( append a y ) ) 5. ( multiple-value-list ( a a ) ) 6. ( b c ) 7. ( set ( car a ) (cdr a) ) 8. ( setf ( car a ) (cdr a) ) a = B. The following forms are entered into the Lisp interpreter and evaluated. (defun f1 ( v1 ) ( f2 v1 ) )

2009 November 30 Lisp Basic Example Test Questions Page 6 of 7 23. (defun f2 ( v2 ) ( 1+ v1 ) ) Under an environment with static scoping what do the following forms evaluate to? 1. ( f1 6 ) 2. ( setq v1 10) ( f1 6) 3. For 2 above what does the environment look like in f2? Under an environment with dynamic scoping what do the following forms evaluate to? 1. ( f1 6 ) 2. ( setq v1 10) ( f1 6) For 2 above what does the environment look like in f2? A. Write a recursive Lisp function nodups(alist), which takes one argument, a list, and returns a list with any consecutive identical top-level items removed. For example: (nodups ( a a a b c c d ) ) should return ( a b c d) (nodups ( a a b a c c ) ) should return ( a b a c) Note: for this question, you may use only the Lisp functions defun, cond, null, car, cdr, cons, and equal. You may use combinations of car and cdr such as caddr. 2. B. Write a recursive Lisp function dups(alist, N), which takes two arguments, a list and an integer, and returns a list with every top-level item in alist duplicated N times. Assume N 1. For example: (dups ( a b c ) 3 ) should return ( a a a b b b c c c) (dups ( a ( ) ( a ) ) 2 ) should return ( a a nil nil ( a ) ( a ) ) Note: for this question, you may use only the Lisp functions defun, cond, null, car, cdr, cons, append, list, +, - and =. You may use combinations of car and cdr such as caddr. You may write support functions, although you get lower evaluation if you do. The only functions you may use are the Lisp functions car and cdr and longer abbreviations, cons, cond, equal, atom, null and specific functions mentioned in each part. B Define a recursive Lisp function, dup, which checks whether its argument is a list containing two successive elements at the top level that are equal. (dup (A B B C) t (dup (A (B) B C) nil 25. ]Consider the following Lisp function. (defun mystery (s) (cond ((null s) 1) ((atom s) 0) (t (max (+ mystery (first s)) 1) (mystery (rest s))))

2009 November 30 Lisp Basic Example Test Questions Page 7 of 7 )) Explain in general what the function mystery returns.