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

Similar documents
Lisp Basic Example Test Questions

Functional programming with Common Lisp

Midterm Examination (Sample Solutions), Cmput 325

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

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

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

Lecture Notes on Lisp A Brief Introduction

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

Imperative, OO and Functional Languages A C program is

Review of Functional Programming

A little bit of Lisp

Common LISP-Introduction

Introduction to Functional Programming

Lisp. Versions of LISP

A Genetic Algorithm Implementation

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

19 Machine Learning in Lisp

Functions, Conditionals & Predicates

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

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

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

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

by the evening of Tuesday, Feb 6

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

Functional Programming - 2. Higher Order Functions

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

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

Associative Database Managment WIlensky Chapter 22

Functional Programming. Pure Functional Languages

Associative Database Managment

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

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

CSCI337 Organisation of Programming Languages LISP

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

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

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

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

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

Recursion & Iteration

Name: CSCI E-220: Artificial Intelligence Midterm Exam, Fall Term 2001

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

CS 314 Principles of Programming Languages

Functional Programming. Contents

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

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Lecture #2 Kenneth W. Flynn RPI CS

Functional Programming. Pure Functional Languages

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

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

Chapter 1. Fundamentals of Higher Order Programming

A Quick Introduction to Common Lisp

MIDTERM EXAMINATION - CS130 - Spring 2005

Common Lisp. Blake McBride

Heap storage. Dynamic allocation and stacks are generally incompatible.

a little more on macros sort of like functions, but..

Structure and Interpretation of Computer Programs

UMBC CMSC 331 Final Exam

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

Pattern Matching WIlensky Chapter 21

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

Announcements. Today s Menu

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

Scheme Basics > (butfirst '(help!)) ()

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

15 Unification and Embedded Languages in Lisp

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

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

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages

CS 275 Name Final Exam Solutions December 16, 2016

Structure and Interpretation of Computer Programs

UMBC CMSC 331 Final Exam

Problem Set 4: Streams and Lazy Evaluation

Module 10: General trees

Documentation for LISP in BASIC

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

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

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Common LISP Tutorial 1 (Basic)

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Allegro CL Certification Program

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

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

FP Foundations, Scheme

Lambda Calculus see notes on Lambda Calculus

6.001 SICP. Types. Types compound data. Types simple data. Types. Types procedures

CS 314 Principles of Programming Languages. Lecture 16

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

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

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

Project 2: Scheme Interpreter

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

INF4820. Common Lisp: Closures and Macros

Introduction to Lisp

Functional Programming. Big Picture. Design of Programming Languages

Transcription:

Lisp: Question 1 Write a recursive lisp function that takes a list as an argument and returns the number of atoms on any level of the list. For instance, list (A B (C D E) ()) contains six atoms (A, B, C, D, E, and NIL). (defun count-atoms (x) (cond ((null x) 0) ;; No more children. ((not (listp x)) 1) ;; Terminal node. (t (+ (if (atom (first x)) 1 (count-atoms (first x))) ;; Break the problem down into two subproblems. (count-atoms (rest x)))))) Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, 2014 1 / 13

Question 1: count-atoms [2]> (count-atoms (A B C)) 3 [3]> (count-atoms (A B C nil)) 4 [4]> (count-atoms (A B C (nil (A B)))) 6 [5]> (count-atoms (A B C (nil (A B ())))) 7 [6]> (count-atoms (())) 1 [7]> (count-atoms ((()))) 1 [8]> (count-atoms ((()) A B C)) 4 [9]> Dr. Zoran Duric () Midterm Review 1 2/ 13 September 23, 2014 2 / 13

Question 2: last5 Write a lisp function last5 that takes a list A as its argument and returns a list B consisting of the last five elements of A. You are not allowed to use the built-in function last. (last5 (A B C)) should return (A B C) (last5 (A B C D E F G H)) should return (D E F G H) (defun last5 (x) (cond ((null (rest (rest (rest (rest (rest x)))))) x) (t (last5 (rest x))))) [3]> (last5 (1 2 3 )) (1 2 3) [4]> (last5 (1 2 3 4 5 6 7 8 9 10 11)) (7 8 9 10 11) [5]> (last5 nil) NIL Dr. Zoran Duric () Midterm Review 1 3/ 13 September 23, 2014 3 / 13

Question 3: flip Write a recursive function flip that takes a binary tree as input and returns a binary tree that it is its mirror image. You can represent binary trees as nested structures: Nested (recursive) representation: (<root> (<left subtree>) (<right subtree>)) Examples: (flip (1 2 3)) should return (1 3 2) (flip (1 (2 3 4) ())) should return (1 () (2 4 3)) (flip (1 (2 (3 4 5) (10 11 12)) (6 () (7 () 8)))) should return (1 (6 (7 8 ()) ()) (2 (10 12 11) (3 5 4))) Dr. Zoran Duric () Midterm Review 1 4/ 13 September 23, 2014 4 / 13

Question 3: flip (defun flip (x) (list (first x) (if (atom (third x)) (third x) (flip (third x))) (if (atom (second x)) (second x) (flip (second x))))) [14]> (flip (1 2 3)) (1 3 2) [15]> (flip (1 (2 3 4) ())) (1 NIL (2 4 3)) [16]> (flip (1 (2 (3 4 5) (10 11 12)) (6 () (7 () 8)))) (1 (6 (7 8 NIL) NIL) (2 (10 12 11) (3 5 4))) [17]> Dr. Zoran Duric () Midterm Review 1 5/ 13 September 23, 2014 5 / 13

Simple Lisp Functions a) Write a lisp function funny first that takes a list of flat lists and returns a new list composed of the first elements of the original flat lists. b) Write a lisp function funny last that takes a list of flat lists as its argument and returns a new list composed of the last elements of the original flat lists. c) Write a lisp function funny len that takes a list of flat lists as its argument and returns the sum of the lengths of the nested lists. d) Write a lisp function funny sum that takes a list of flat lists of numbers and returns the sum of the elements of the nested lists. (funny first ((A B) (C) (D E) (F G H))) should return (A C D F) (funny last ((A B) (C) (D E) (F G H))) should return (B C E H) (funny len ((A B) (C) (D E) (F G H))) should return 8 (funny sum ((1 2) (3) (4 5) (10 20 30))) should return 75 Dr. Zoran Duric () Midterm Review 1 6/ 13 September 23, 2014 6 / 13

Simple Lisp Functions: Answers (defun funny_first (x) (mapcar # (lambda (y) (first y)) x)) (defun funny_last (x) (mapcar # (lambda (y) (first (last y))) x)) (defun funny_len (x) (apply # + (mapcar # (lambda (y) (length y)) x))) (defun funny_sum (x) (apply # + (mapcar # (lambda (y) (apply # + y)) x))) [30]> (funny_first ((A B) (C) (D E) (F G H))) (A C D F) [31]> (funny_last ((A B) (C) (D E) (F G H))) (B C E H) [32]> (funny_len ((A B) (C) (D E) (F G H))) 8 [33]> (funny_sum ((1 2) (3) (4 5) (10 20 30))) 75 Dr. Zoran Duric () Midterm Review 1 7/ 13 September 23, 2014 7 / 13

Question: ListNonNumbers Write a lisp function that takes a flat list as an argument and returns a list whose elements are those elements of the original list that are not numbers. (defun ListNonNumbers (x) (mapcan # (lambda (y) (if (numberp y) nil (list y))) x)) [40]> (ListNonNumbers (A B C D 3 5 6)) (A B C D) [41]> (ListNonNumbers (A B C D 3 5 6 (2 3 4))) (A B C D (2 3 4)) [42]> (ListNonNumbers (A B C D 3 5 6 (2 3 4) nil)) (A B C D (2 3 4) NIL) [43]> (ListNonNumbers nil) NIL Dr. Zoran Duric () Midterm Review 1 8/ 13 September 23, 2014 8 / 13

Question: AddNumbers Write a lisp function that takes a flat list as an argument and returns a sum of the numbers in the original list. Your function should not add the non-number elements of the original list. (defun AddNumbers (x) (apply # + (mapcar # (lambda (y) (if (numberp y) y 0)) x))) [45]> (AddNumbers (A B C D 3 5 6 (2 3 4) nil)) 14 [46]> (AddNumbers (A B C D 3 5 6)) 14 [47]> (AddNumbers (1 2 3 4 5 6)) 21 [48]> (AddNumbers (A B C D nil (2 3 4))) 0 Dr. Zoran Duric () Midterm Review 1 9/ 13 September 23, 2014 9 / 13

Question: d shuffle Write a lisp function d shuffle that takes a list of 32 different symbols and returns a list in which the first 16 original symbols are interleaved with the second 16 original symbols, i.e. list (s 1 s 2 s 3 s 4... s 29 s 30 s 31 s 32 ) becomes (s 1 s 17 s 2 s 18... s 15 s 31 s 16 s 32 ). (defun d_shuffle (l) (do ((newl nil) (i 15 (- i 1))) ((< i 0) newl) (setf newl (cons (nth i l) (cons (nth (+ i 16) l) newl))))) [53]> (d_shuffle (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32)) (1 17 2 18 3 19 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 32) [54]> Dr. Zoran Duric () Midterm Review 1 10/ 13 September 23, 2014 10 / 13

Water-Jug Puzzles In the water-jug puzzle we are given a 4-liter jug, and a 7-liter jug. Initially, both jugs are empty. Either jug can be filled with water from a tap, and we can discard water from either jug down a drain. Water may be poured from one jug into the other. There is no additional measuring device. We want to find a set of operations that will leave precisely x liters of water in either one of the jugs. i. Set up a state-space search formulation of the water jug puzzle: a) Given the initial iconic state description as a data structure. b) Give a goal condition on states as some test on data structures. c) Name the operators on states and give precise descriptions of what each operator does to a state description. ii. Find whether the goals x = {1, 2, 3, 4, 5, 6, 7} can be accomplished in 8 or fewer steps. Hint: Use breadth-first search. Dr. Zoran Duric () Midterm Review 1 11/ 13 September 23, 2014 11 / 13

Water-Jug Puzzle a) (A B) // A is the amount in the 4-liter jug // B in the 7-liter jug b) (A == x) or (B == x) c) FA: (4 B), FB: (A 7) EA: (0 B), EB: (A 0) PAB: if ((A+B)<= 7) then (0 A+B) else (A+B-7 7) PBA: if ((A+B)<=4) then (A+B 0) else (4 A+B-4) Dr. Zoran Duric () Midterm Review 1 12/ 13 September 23, 2014 12 / 13

Water-Jug Puzzle Solution Dr. Zoran Duric () Midterm Review 1 13/ 13 September 23, 2014 13 / 13