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

Similar documents
Lisp Basic Example Test Questions

Lisp. Versions of LISP

Common LISP-Introduction

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

Announcements. Today s Menu

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

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

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

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

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

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

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp

Lecture Notes on Lisp A Brief Introduction

Midterm Examination (Sample Solutions), Cmput 325

Imperative, OO and Functional Languages A C program is

Sample Final Exam Questions

Review of Functional Programming

Functional programming with Common Lisp

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

CS 314 Principles of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Pure Functional Languages

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

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

A Brief Introduction to Scheme (II)

User-defined Functions. Conditional Expressions in Scheme

Documentation for LISP in BASIC

A little bit of Lisp

CS 314 Principles of Programming Languages. Lecture 16

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

Lecture #2 Kenneth W. Flynn RPI CS

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

Functional Programming. Contents

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

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

Functional Programming. Pure Functional Languages

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

Project 2: Scheme Interpreter

by the evening of Tuesday, Feb 6

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

FP Foundations, Scheme

Recursion & Iteration

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

Scheme Quick Reference

A Genetic Algorithm Implementation

Functional Programming. Pure Functional Programming

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Modern Programming Languages. Lecture LISP Programming Language An Introduction

An Explicit-Continuation Metacircular Evaluator

Imperative languages

Building a system for symbolic differentiation

Introduction to Functional Programming and basic Lisp

Scheme Quick Reference

MIDTERM EXAMINATION - CS130 - Spring 2005

CS61A Midterm 2 Review (v1.1)

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

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

Lists in Lisp and Scheme

John McCarthy IBM 704

Simplifying Logical Formulas

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

Functional Programming - 2. Higher Order Functions

Common Lisp. Blake McBride

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

CSC 533: Programming Languages. Spring 2015

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

CSCI337 Organisation of Programming Languages LISP

Associative Database Managment WIlensky Chapter 22

Associative Database Managment

Introduction to Functional Programming

UMBC CMSC 331 Final Exam

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

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

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

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

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

Heap storage. Dynamic allocation and stacks are generally incompatible.

Pattern Matching WIlensky Chapter 21

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CMSC 331 Final Exam Section 0201 December 18, 2000

Chapter 15 Functional Programming Languages

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

UMBC CMSC 331 Final Exam

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

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

CSE 341 Section Handout #6 Cheat Sheet

ALISP interpreter in Awk

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

CMSC 331 Final Exam Section 0201 December 18, 2000

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Putting the fun in functional programming

Lambda Calculus see notes on Lambda Calculus

Introduction to Scheme

Building a system for symbolic differentiation

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

Introductory Scheme. Revision 1

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

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

Transcription:

Homework Solutions: Problem Set Homework 2 Due uesday September 8, 2015 in class Be sure to follow the guidelines for Programming Assignments. Since these problems are simple, you may skip the Statement of the Problem, Approach and Algorithm - Program Flow sections and give an overall conclusion for the entire programming assignment. Make sure you give me a listing of each program and at least three test cases for each. You may wish to use the "dribble" option in the file menu of XLISP to capture the screen as you test your functions. Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below) Problems 4-1, 4-5 and 4-6 in Chapter 4 of Winston and Horn's LISP (see Below) 3.1 Define EXCHANGE, a procedure that returns the elements of a two-element in reverse order. Lisp-> (exchange (adam eve)) (eve adam) 3-1 (defun EXCHANGE (lis) (cond ( (null lis) nil) ( (= (length lis) 2) (list (cadr lis) (car lis)) ) ( t 'Error) )) 3.3 Define ROAE-LEF, a procedure that takes a list as its argument and returns a new list in which the former first element becomes the last. Lisp-> (rotate-left (a b c)) (b c a) Lisp-> (rotate-left (rotate-left (a b c))) (c a b) 3-3 (defun ROAE-LEF (lis) (append (cdr lis) (list (car lis)) )) 3.4 Define ROAE-RIGH which is like ROAE-LEF except in the pposite direction. You might want to use BULAS. 3-4 (defun ROAE-RIGH (lis) (append (last lis) (butlast lis) )) 3.5 A palindrome is a list that has the same sequence of elements when read from right-to-left that it does from left-to-right. Define PALINDROME such that it takes a list as its argument and returns a plaindrome that is twice as long. 3-5 (defun PALINDROMIZE (lis) (cond ( (null lis) nil) ( t (append lis (reverse lis))) )) 1

4.1 Define DIVISIBLE-BY-HREE, a predicate that determines if an integer is divisible by three. You may want to use REM. Lisp-> (divisible-by-three 3) Lisp-> (divisible-by-three 4) Nil > (DEFUN DIVISIBLE-BY-HREE (n) (cond ((eq n 0)nil) ((eq (rem n 3) 0) t) ('else nil))) DIVISIBLE-BY-HREE > (mapcar 'divisible-by-three '(1 2 3 4 5 6 7 8 9 10)) ( ) > (divisible-by-three 0) 4.5 Write IF forms that are equivalent to (ABS X), (MIN A B), and (MAX A B), where X, A,and B are numbers. 4.6 Compose COND forms that are equivalent to (NO U), (OR X Y Z), and (AND A B C). 2

XLISP-PLUS version 3.02 Portions Copyright (c) 1988, by David Betz. Modified by homas Almy and others. XLISP-SA Release 3.52.4 (Beta). Copyright (c) 1989-1998, by Luke ierney. >(pprint (function-lambda-expression #'exchange)) (COND ((NULL LIS) ) ((= (LENGH LIS) 2) (LIS (CADR LIS) (CAR LIS))) ( (QUOE )))) > (exchange '()) > (exchange '(a)) > (exchange '(a b)) > (exchange '(a b c)) > (pprint (function-lambda-expression #'construct)) (LAMBDA (F R) (CONS F R)) > (construct 'a 'b) (A. B) > (construct 'a '()) (A) > (construct 'a '(b)) (A B) > (construct nil '(a b)) ( A B) > (construct '(a) '(b c)) ((A) B C) > (pprint (function-lambda-expression #'rotate-left)) (APPEND (CDR LIS) (LIS (CAR LIS)))) > (rotate-left '(a b c)) (B C A) > (rotate-left '()) () > (rotate-left '(a b)) > (pprint (function-lambda-expression #'rotate-right)) (APPEND (LAS LIS) (BULAS LIS))) > (rotate-right '(a b c)) (C A B) > (rotate-right '(a b)) > (rotate-right '(a)) (A) > (rotate-right nil) > (rotate-right '()) 3

> (pprint (function-lambda-expression #'palindromize)) (COND ((NULL LIS) ) ( (APPEND LIS (REVERSE LIS))))) > (palindromize '()) > (palindromize '(a)) (A A) > (palindromize '(a b)) (A B B A) > (pprint (function-lambda-expression #'f-to-c)) (LAMBDA (F) (/ (* 5 (- F 32)) 9)) > (f-to-c 32) 0 > (f-to-c 85) 29.444444444444444 > (f-to-c 212) 100 > (f-to-c 94) 34.44444444444444 > (pprint (function-lambda-expression #'c-to-f)) (LAMBDA (F) (+ (/ (* 9 F) 5) 32)) > (c-to-f 0) 32 > (c-to-f 100) 212 > (c-to-f 34.44) 93.991999999999984 > (pprint (function-lambda-expression #'palindromep)) (EQUAL LIS (REVERSE LIS))) > (palindromep '(a b c b a)) > (palindromep '(a b c c b a)) > (palindromep '(a b c c c b a)) > (palindromep '(a b c c a)) > (pprint (function-lambda-expression #'rightp)) (LAMBDA (H S1 S2) (< (ABS (- (** H 2) (** S1 2) (** S2 2))) (* 0.02 (** H 2)))) > (rightp 5 4 3) > (rightp 1.414 1 1) > (rightp 5 3.9 3) > (rightp 5 3.94 3) > (rightp 5 3.93 3) 4

> (pprint (function-lambda-expression #'check-temp)) (LAMBDA (EMP) (COND ((< EMP 0) (QUOE OO_COLD)) ((> EMP 100) (QUOE OO_HO)) ( (QUOE )))) > (check-temp 0) > (check-temp 100) > (check-temp 50) > (check-temp 101) OO_HO > (check-temp -1) OO_COLD > (pprint (function-lambda-expression #'mygcd)) (LAMBDA (N1 N2) (COND ((AND (INEGERP N1) (INEGERP N2)) (GCD N1 N2)) ( (QUOE )))) > (mygcd 12 4) 4 > (mygcd 12.1 4.1) > (mygcd 'a 4) > (mygcd 12 'b) > (mygcd 12.0 4.0) > (exit) 5