How To Program In Scheme (Prolog)



Similar documents
Basic Lisp Operations

Chapter 15 Functional Programming Languages

Functional Programming

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

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.

Bash shell programming Part II Control statements

The C Programming Language course syllabus associate level

Programming Languages CIS 443

Moving from CS 61A Scheme to CS 61B Java

Programming Languages in Artificial Intelligence

Introduction to Python

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

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

CS1102: Adding Error Checking to Macros

Universitetet i Linköping Institutionen för datavetenskap Anders Haraldsson

Example of a Java program

SIMPLIFYING ALGEBRAIC FRACTIONS

Programming Languages

7.1 Our Current Model

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

CS 106 Introduction to Computer Science I

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Logic in general. Inference rules and theorem proving

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Visual Basic Programming. An Introduction

Unix Scripts and Job Scheduling

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

J a v a Quiz (Unit 3, Test 0 Practice)

Computer Programming I

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

CS 1133, LAB 2: FUNCTIONS AND TESTING

Exercise 1: Python Language Basics

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Eventia Log Parsing Editor 1.0 Administration Guide

Compiler I: Syntax Analysis Human Thought

Appendix... B. The Object Constraint

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Lecture 1: Introduction

COMPUTER SCIENCE TRIPOS

High-Level Language. Building a Modern Computer From First Principles.

How To Understand The Theory Of Computer Science

Introduction to Java

CIS 500 Software Foundations Midterm I, Review Questions

The Halting Problem is Undecidable

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Selection Statements

Crash Course in Java

R Language Definition

2. Abstract State Machines

Sources: On the Web: Slides will be available on:

Visual Logic Instructions and Assignments

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

CSCI 1100 Computer Science 1 Homework 1 Calculations and Functions

JavaScript: Control Statements I

Introduction to formal semantics -

CISC 181 Project 3 Designing Classes for Bank Accounts

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

Introduction to Matlab

Microsoft Access 3: Understanding and Creating Queries

Notes on Complexity Theory Last updated: August, Lecture 1

CSC 221: Computer Programming I. Fall 2011

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

COLLEGE ALGEBRA 10 TH EDITION LIAL HORNSBY SCHNEIDER 1.1-1

CHAPTER 7 GENERAL PROOF SYSTEMS

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

PHP Tutorial From beginner to master

THE SET ANALYSIS. Summary

Java Basics: Data Types, Variables, and Loops

Web Programming Step by Step

Chapter 6: Programming Languages

Lecture 4: Writing shell scripts

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Introduction to Python

MATLAB Programming. Problem 1: Sequential

ECE 122. Engineering Problem Solving with Java

The Classes P and NP

Compiler Construction

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

BASH Scripting. A bash script may consist of nothing but a series of command lines, e.g. The following helloworld.sh script simply does an echo.

Some programming experience in a high-level structured programming language is recommended.

The previous chapter provided a definition of the semantics of a programming

GCF/ Factor by Grouping (Student notes)

C A R I B B E A N E X A M I N A T I O N S REPORT ON CANDIDATES S WORK IN THE CARIBBEAN SECONDARY EDUCATION CERTIFICATE EXAMINATION MAY/JUNE 2012

Python Evaluation Rules

02-201: Programming for Scientists

Boolean Algebra Part 1

Sample CSE8A midterm Multiple Choice (circle one)

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Certified PHP Developer VS-1054

Lecture 2 Notes: Flow of Control

Transcription:

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction Next up: Numeric operators, REPL, quotes, functions, conditionals Types and values Syntax and semantics Exceptions Logic programming: Prolog Announcements Reminder: Term Test 1 is on Monday in GB405, not in the regular lecture room. 50 minutes (11:10 12:00. You're allowed to have one double-sided aid sheet for the test. You must use standard letter-sized (that is, 8.5" x 11" paper. The aid sheet can be produced however you like (typed or handwritten. Bring your TCard. What's covered? Everything from the first seven lectures (that is, everything up to and including September 26th. Lab 1. An old Term Test 1 has been posted. The exercises at the end of each lecture are also good practice. Solutions to Lab 1 are available from the directory ~ajuma/share/326/lab1 on ECF. 1 2 Review: car, cdr, and cons Numeric operators car returns the first element of a list: > (car '((a b (c d (a cdr returns the rest of a list: > (cdr '((a b (c d (b (c d cons adds an element to the front of a list: The numeric operators +, -, *, / are used just like any function, in parenthesized prefix form. > (+ 5 3 8 > (- 5 3 2 > (* 5 3 15 > (/ 5 3 ; returns a fraction! 5/3 > (cons '(a b '((c d ((a b (c d 3 4

More examples: > (* (+ 5 3 (/ 3 2 12 > (+ 5 6 7 8 26 > (- 5 3 4 2-4 > (/ 16 2 4 2 Numeric operators > (* (+ 1 2 3 (* (+ 1 2 (- 4 2 2 72 Number comparisons Number comparisons are performed using the functions =, <, >, <=, and >=. > (= 4 5 > (>= 4 4 > (= (+ 2 1 (- 4 1 (/ 6 2 > (< 5 6 7 ; checks if 5 < 6 < 7 5 6 Type-checking functions The functions number?, symbol?, and list? check the type of the given argument and return boolean values ( or. > (number? 5 > (number? 'sam > (symbol? 'sam > (symbol? 5 > (list? '(a b > (list? (+ 3 4 > (list? '(+ 3 4 > (list? 7 Other useful functions The function zero? returns true iff given the number 0. > (zero? 0 > (zero? (- 3 3 > (zero? (* 3 1 > (zero? '(- 3 3 zero?: expects argument of type <number>; given (- 3 3 7 8

Other useful functions The function null? returns true iff given an empty list. > (null? '( > (null? '(a > (null? (car '(a > (null? (cdr '(a The empty list, or nil Some Schemes treat ( as equivalent to. mzscheme does not. The Scheme standard says you should quote (: '( mzscheme is more relaxed. > ( ( > '( ( 9 10 Boolean operations: and, or, not Like the and and or operators in Python, the and and or operators in Scheme are short-circuited: they evaluate only as much as needed. and stops at the first false condition. or stops at the first true condition. > (and (zero? 0 (number? 2 (eq? 1 1 > (and (zero? 0 (number? 'x (eq? 1 2 > (or (symbol? 'x (symbol? 3 Boolean operations: and, or, not As in Python, the and and or operators in Scheme return the last thing they evaluate. Everything except is treated as "true". > (or (symbol? 1 "no" "other" "no" > (or 3 3 > (and 3 4 "hi" "bye" 11 12

Boolean operations: and, or, not The not operation always returns or. READ-EVAL-PRINT Loop (REPL The Scheme interpreter runs in a Read-Evaluate-Print Loop (REPL. > (not (null? '(1 2 > (not 3 Since Scheme doesn't have a numeric "not equals" operator (like the!= operator in C/Java/Python, we have to combine not and = in order to evaluate "not equals". > (not (= 3 4 > (not (= (+ 4 5 (* 3 3 READ: Read input from user. e.g. The user enters a function application EVAL: Evaluate input: (f arg1 arg2 argn. 1. Evaluate f to obtain a function. 2. Evaluate each argi to obtain a value. 3. Apply function to argument values. PRINT: Print resulting value. e.g. Print the result of the function application And then READ the next input from the user. 13 14 READ-EVAL-PRINT Loop Example READ-EVAL-PRINT Loop Example Let's go through the REPL for the following interaction. > (cons 'a (cons 'b '(c d (a b c d 1. Read the function application: (cons 'a (cons 'b '(c d 2. Evaluate cons to obtain a function cons evaluates to a built-in function 3. Evaluate 'a to obtain a itself. 4. Evaluate (cons 'b '(c d:! (a Evaluate cons to obtain a function.! (b Evaluate 'b to obtain b itself! (c Evaluate '(c d to obtain (c d itself! (d Apply the cons function to b and (c d to obtain!! (b c d 5. Apply the cons function to a and (b c d to obtain (a b c d 6. Print the result of the application: (a b c d 7. Display the prompt in order to read the next input from the user. > 15 16

The quote issue The quote issue (list? (a b undefined identifier: a > (list? (quote (a b > (list? '(a b > (hi undefined identifier: hi > (quote hi hi > 'hi hi In the context of REPL, quote inhibits evaluation. > (cons 'a (cons 'b '(c d (a b c d > (cons 'a '(cons 'b '(c d ; quote before 2nd arg (a cons 'b '(c d > (cons a (cons 'b '(c d ; no quote before 1st arg reference to undefined identifier: a Observe that 'x means the same thing as (quote x. 17 18 The quote issue REPL and function definition Some things evaluate to themselves. For these things, quoting has no effect (and is unnecessary. > (list '1 '42 ' ' '( (1 42 ( > (list 1 42 ( (1 42 ( Note that the list function constructs a list that consists of the arguments it's given. The basics are still the same when you're defining a function, but the interpretation is a little different. READ: Read input from user. This time the input is a symbol definition rather than a function application. EVAL: Evaluate input. Store the function definition PRINT: Print resulting value. The symbol defined, or perhaps nothing. mzscheme prints nothing for a function definition. > (define (square x (* x x > 19 20

Function definition syntax 1 (define (<function-name> <function-parameters>! <expression> Function definition syntax 2 (define <function-name> <function-value> > (define (square x (* x x > (square 2 4 > (define (mean x y (/ (+ x y 2 > (mean 5 11 8 > (mean 1 2 3 procedure mean: expects 2 arguments, given 3: 1 2 3 The "function value" is a created using a lambda expression. A lambda expression evaluates to an unnamed function. "evaluates to an unnamed function": just like an arithmetic expression (like (+ 5 3 evaluates to an unnamed number. > (define square (lambda (x (* x x > (define mean (lambda (x y (/ (+ x y 2 In the examples above, we use define to give a name to the unnamed functions produced by the lambda expressions. 21 22 Lambda calculus Conditional Execution: if A formal system for defining functions and their properties. Equivalent to Turing machines. That is, equivalent to any general computing machine. Since a lambda expression evaluates to a function, we can (if we want to evaluate this function immediately, without ever giving it a name. > ((lambda (x x 'a a > ((lambda (x (* x x 2 4 > ((lambda (x y (+ x y 3 4 7 (if <condition> <result1> <result2> Semantics: 1. Evaluate <condition>. 2. If result is true (non-, then evaluate and return <result1>. 3. Otherwise, evaluate and return <result2>. > (define (abs-val x (if (>= x 0 x (- x > (abs-val -4 4 > (abs-val (- (* 3 4 4 8 23 24

Conditional Execution: cond Examples with cond (cond (<condition1> <result1> (<condition2> <resultt2> (<conditionn> <resultn> (else <else-result> ;optional else clause Semantics: 1. Evaluate conditions in order until one of them returns a true value. 2. Evaluate and return the corresponding result. 3. If none of the conditions return a true value, evaluate and return <else-result>. 4. If none of the conditions return a true vale and there is no else clause, the result of the cond expression is unspecified. > (define (abs-val x (cond ((>= x 0 x (else (- x > (define (grade n (cond ((>= n 80 'A > (grade 75 b > (grade 45 f ((>= n 70 'B ((>= n 60 'C ((>= n 50 'D (else 'F 25 26 if vs cond if vs cond Let's define atom?, a function that takes a parameter x and returns if x is an atom, and otherwise. First, we use cond: (define (atom? x (cond ((symbol? x ((number? x ((char? x ((string? x ((null? x (else Now we write atom? using if: (define (atom? x (if (symbol? x (if (number? x (if (char? x (if (string? x (if (null? x 27 28

Better atom? functions An atom is anything except a non-empty list. (define (atom? x (if (null? x (if (list? x (define (atom? x (if (list? x (null? x Even better atom? functions (define (atom? x (or (null? x (not (list? x (define (atom? x (cond ((null? x ((list? x (else 29 30 Exercises Define a function f that takes two numbers x and y as input, and returns a list containing the numbers x+y, x-y, x/y, and x*y. Define a function g that takes two lists L1 and L2 as input, and returns a new list formed by adding the first two elements of L2 to the beginning of L1. Define a function everyothersum that takes a list L of numbers as input, and returns the sum of every second number in the list, starting with the first number. 31