Writing Functions in Scheme. Writing Functions in Scheme. Checking My Answer: Empty List. Checking My Answer: Empty List



Similar documents
Party Time! Computer Science I. Signature Examples PAIR OBJ. The Signature of a Procedure. Building a Set. Testing for Membership in a Set

Hadoop and Map-reduce computing

Programming Languages CIS 443

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

How To Program In Scheme (Prolog)

Roman Numerals Case Study 1996 M. J. Clancy and M. C. Linn

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Chapter 2: Elements of Java

Chapter 15 Functional Programming Languages

3. Mathematical Induction

Two's Complement Adder/Subtractor Lab L03

Chapter 7 Uncomputability

Why should I back up my certificate? How do I create a backup copy of my certificate?

The Correlation Coefficient

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Unit 11 Fractions and decimals

Basic Lisp Operations

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

Regression Verification: Status Report

Transformer circuit calculations

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

ERP guide and questions

Reading 13 : Finite State Automata and Regular Expressions

Why do you want to launch a business analyst career? Some possibilities include:

SAS Comments How Straightforward Are They? Jacksen Lou, Merck & Co.,, Blue Bell, PA 19422

5.4 Solving Percent Problems Using the Percent Equation

2. Abstract State Machines

Database Security. The Need for Database Security

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

CS 1133, LAB 2: FUNCTIONS AND TESTING

The essential guide to automatic enrolment

Data Integration through XML/XSLT. Presenter: Xin Gu

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

Describing Relationships between Two Variables

UNIVERSITY OF CALIFORNIA College of Engineering Department of Electrical Engineering and Computer Sciences

COLLEGE ALGEBRA 10 TH EDITION LIAL HORNSBY SCHNEIDER 1.1-1

Section IV.1: Recursive Algorithms and Recursion Trees

NEW TECHNIQUE TO DEAL WITH DYNAMIC DATA MINING IN THE DATABASE

Computational Models Lecture 8, Spring 2009

Programming Project 1: Lexical Analyzer (Scanner)

Calculate Highest Common Factors(HCFs) & Least Common Multiples(LCMs) NA1

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Certification Authorities Software Team (CAST) Position Paper CAST-13

8 Primes and Modular Arithmetic

Understanding Sql Injection

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

LogProcess v1.0 User Guide

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

TEMPLATES FOR PROGRESSIVE PAIRING LADDER

TinyUrl (v1.2) 1. Description. 2. Configuration Commands

How To Develop An Application

This can dilute the significance of a departure from the null hypothesis. We can focus the test on departures of a particular form.

Principles of Data Visualization

Dynamic Programming Problem Set Partial Solution CMPSC 465

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

CS558. Network Security. Boston University, Computer Science. Midterm Spring 2014.

Life Insurance Modelling: Notes for teachers. Overview

2. Capitalize initial keyword In the example above, READ and WRITE are in caps. There are just a few keywords we will use:

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

FIPA Agent Management Specification

Authentication and Encryption: How to order them? Motivation

A Guide to Process Mapping

Compound Data in Java. Instances of Compound Data Types. Armadillos. Design: Functional to Object-Oriented. Beginner Scheme:

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

General instructions for the content of all StatTools assignments and the use of StatTools:

Assignment Kits. Summary Kit Contents Lecture 1: Kit cover sheet (page 40)

VFComb 1.3 the program which simplifies the virtual font management

Chapter 5. Selection 5-1

Template: Family Reunion: T-Shirt numbers

Chapter 7: Functional Programming Languages

Basic Set Theory. 1. Motivation. Fido Sue. Fred Aristotle Bob. LX Semantics I September 11, 2008

A GUIDE TO STARTING UP AS A SOLE TRADER/SELF EMPLOYED

Objectives After completion of study of this unit you should be able to:

Contents. Pentaho Corporation. Version 5.1. Copyright Page. New Features in Pentaho Data Integration 5.1. PDI Version 5.1 Minor Functionality Changes

How To Understand The Theory Of Computer Science

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

IBI Group FTP: Usage Instructions

Test Driven Development

STUDY PLUS MATHS ON THE FARM LESSON 1

Chapter 6. Data-Flow Diagrams

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Modern Systems Analysis and Design

Calculating interest rates

CS1102: Adding Error Checking to Macros

7 Relations and Functions

Apple Certificate Library Functional Specification

Basic Concepts of Set Theory, Functions and Relations

Transcription:

Writing Functions in Scheme Writing Functions in Scheme Suppose we want a function ct which takes a list of symbols and returns the number of symbols in the list (ct (a b c)) 3 (ct ()) 0 (ct (x y z w t)) 5 How can we write this function? Answer #1: Have the instructor write it ;; ct : <list-of-sym> -> <num> ;; (ct ()) 0 ;; (ct (a b c)) 3 [else (ct (cdr l)))])) Checking My Answer: Empty List Checking My Answer: Empty List [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct ()) [(null? ()) 0] [else (ct (cdr ())))]) [(null? ()) 0] [else (ct (cdr ())))]) [#t 0] [else (ct (cdr ())))]) 1-4

Checking My Answer: Empty List [else (ct (cdr l)))])) [#t 0] [else (ct (cdr ())))]) [else (ct (cdr l)))])) 0 [else (ct (cdr l)))])) (ct (a b c)) [else (ct (cdr l)))])) [(null? (a b c)) 0] [else (ct (cdr (a b c))))]) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (a b c)) 0] [else (ct (cdr (a b c))))]) [else (ct (cdr (a b c))))]) [else (ct (cdr (a b c))))]) (ct (cdr (a b c)))) 5-8

[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (a b c)))) (ct (b c))) (ct (b c))) [(null? (b c)) 0] [else (ct (cdr (b c))))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (b c)) 0] [else (ct (cdr (b c))))])) [else (ct (cdr (b c))))])) [else (ct (cdr (b c))))])) (ct (cdr (b c))))) 9-12

[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (b c))))) (ct (c)))) (ct (c)))) [(null? (c)) 0] [else (ct (cdr (c))))]))) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? (c)) 0] [else (ct (cdr (c))))]))) [else (ct (cdr (c))))]))) [else (ct (cdr (c))))]))) (ct (cdr (c)))))) 13-16

[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) (ct (cdr (c)))))) (ct ())))) (ct ())))) [(null? ()) 0] [else (ct (cdr ())))])))) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [(null? ()) 0] [else (ct (cdr ())))])))) [#t 0] [else (ct (cdr ())))])))) [#t 0] [else (ct (cdr ())))])))) 0))) 17-20

[else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) [else (ct (cdr l)))])) 0))) 1)) 1)) 2) Writing Functions in Scheme: Answer #2 [else (ct (cdr l)))])) 2) [else (ct (cdr l)))])) 3 Answer #2: Use the general design recipe Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function Run examples as tests 21-25

Writing Functions in Scheme: Answer #2 Data Definitions Answer #2: Use the general design recipe Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function What is a "list of symbols"? Sometimes the is given, somtimes you have to create it Usually include it in your code as a comment Run examples as tests works 90% of the time Contracts Examples A contract is a comment that identifies set of input values and output values Examples (usually in comments at first) help clarify the purpose of the function ;; ct: <list-of-sym> -> <num> All mentioned data sets should have a somewhere ;; (ct ()) 0 ;; (ct (a b c)) 3 Make sure that every case in the is covered at least once 26-34

Template Template Two cases in implies cond with two cond-lines Template Template Corresponding predicate for each data case Extract parts in cases with meta-variables 35-38

Template Template Recursive call for self-references in A template depends only on the input data; it ignores the function s purpose (Nevertheless, generating a template, which is fairly automatic, usually provides most of the function) Template to Function Template to Function Transform template to function line-by-line Transform template to function line-by-line 39-43

Template to Function Reminder: Recipe Transform template to function line-by-line [(pair? l) (ct (cdr l)) )])) Sometimes, a part of the template isn t needed Locate or write a Write a contract Write examples Create a template that follows the shape of the Convert the template to the final function Run examples as tests Reminder: Template Steps More Examples Create a cond expression with one line for each case in the Write down a predicate for each case For the answer, extract parts in cases with meta-variables For each self-reference in the, add a recursive call (more examples in class) Shape of template shape == Shape of 44-47

Generalized Recipe Locate or write s Write contracts Write examples Create a template that follows the shape of the data definition, one for each Convert the templates to the final functions Run examples as tests 48-49