PRACTICAL COMMON LISP Peter Seibel 1.

Slides:



Advertisements
Similar presentations
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Advertisements

Types and Arithmetic Operators
The Binary Numbering Systems
Study Guides Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM Mycatstudy.com.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Assembly Language and Computer Architecture Using C++ and Java
TES3111 Oct 2001 Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S- Expression enclosed by (…) Atom -
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Number Systems Lecture 02.
Quantitative - Arithmetic - Numbers, Divisibility Test, HCF and LCM
Functional Programming
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
MATH 224 – Discrete Mathematics
Signed Numbers, Powers, & Roots
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
IT253: Computer Organization
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
D ATA T YPE. NUMBERS A number is an immutable type – means that changing or updating its value result in a newly allocated object. There are several types.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Common lisp A functional programming language. Useful URL:
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
CSC 221 Computer Organization and Assembly Language
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
PRACTICAL COMMON LISP Peter Seibel 1.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
B121 Chapter 5 Working with Numbers. Number representation ThousandHundredsTensUnits Natural numbers: 1,2,3,4,5……… Integers: Natural numbers.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans.
28 Formatted Output.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations, decisions and looping
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
CHAPTER 3: String And Numeric Data In Python
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
The Fundamentals of C++
Presentation transcript:

PRACTICAL COMMON LISP Peter Seibel 1

CHAPTER 10 NUMBERS, CHARACTERS, AND STRINGS 2

INTRODUCTION Common Lisp provides built-in support for most of the data types typically found in modern languages: numbers (integer, floating point, and complex), characters, strings, arrays (including multidimensional arrays), lists, hash tables, input and output streams, and an abstraction for portably representing filenames. This chapter will cover the built-in “scalar” data types: numbers, characters, and strings. 3

NUMBERS Lisp was originally designed by a mathematician ( 數學家 ) as a tool for studying mathematical functions. Its numbers behave more like true mathematical numbers than the approximations of numbers. For instance Integers in Common Lisp can be almost arbitrarily large rather than being limited by the size of a machine word. And dividing two integers results in an exact ratio, not a truncated value. Common Lisp also offers several types of floating-point numbers. Common Lisp supports complex numbers—the numbers that result from doing things such as taking square roots and logarithms of negative numbers. 4

NUMERIC LITERALS For a given number of a given type, there can be many different textual representations, all of which will be translated to the same object representation by the Lisp reader. For instance, you can write the integer 10 as 10, 20/2, #xA, or any of a number of other ways, but the reader will translate all these to the same object. CL-USER> CL-USER> 20/2 10 CL-USER> #xa ; 十六進位表示法 #xa - #xf 10 CL-USER> #b1010 ; 二進位表示法 10 CL-USER> #o12 ; 八進位表示法 10 5

NUMERIC LITERALS It’s also possible to write rationals ( 有理數 ) in bases other than 10. If preceded by #B or #b, a rational literal is read as a binary number with 0 and 1 as the only legal digits. An #O or #o indicates an octal number (legal digits 0–7), and #X or #x indicates hexadecimal (legal digits 0–F or 0–f). You can write rationals in other bases from 2 to 36 with #nR where n is the base (always written in decimal). Additional “digits” beyond 9 are taken from the letters A–Z or a–z. #b10101 → 21 #b1010/1011 → 10/11 #o777 → 511 #xDADA → #36rABCDEFGHIJKLMNOPQRSTUVWXYZ →

NUMERIC LITERALS You can also write floating-point numbers in a variety of ways. Common Lisp defines four subtypes of floating-point number: short, single, double, and long. The exponent markers s, f, d, l (and their uppercase equivalents) indicate short, single, double, and long floats, respectively. ( The letter does double duty: it marks the beginning of the exponent and indicates what floating point representation should be used for the number. The letter e indicates that the default representation (initially single-float) should be used. 1.0 → 1.01e0 → 1.0 1d0 → 1.0d → e0 → → → e-3 → E-3 → e20 → 1.23e19 123d23 → 1.23d25 7

NUMERIC LITERALS Complex numbers are written in their own syntax, #C or #c followed by a list of two real numbers representing the real( 實數 ) and imaginary( 虛數 ) part of the complex number. No complex numbers have a rational real component and a zero imaginary part, they’re represented by the appropriate rational value. #c(2 1) → #c(2 1) #c(2/3 3/4) → #c(2/3 3/4) #c(2 1.0) → #c( ) #c( d0) → #c(2.0d0 1.0d0) #c(1/2 1.0) → #c( ) #c(3 0) → 3 #c( ) → #c( ) #c(1/2 0) → 1/2 #c(-6/3 0) → -2 8

BASIC MATH The basic arithmetic operations—addition, subtraction, multiplication, and division—are supported for all the different kinds of Lisp numbers with the functions +, -, *, and /. (+ 1 2) → 3( ) → 6 ( ) → 13.0(+ #c(1 2) #c(3 4)) → #c(4 6) (- 5 4) → 1(- 2) → -2 ( ) → 2(* 2 3) → 6 (* 2 3 4) → 24(/ 10 5) → 2 (/ ) → 1(/ 4) → 1/4 If any of the arguments are complex, any real arguments are converted to the complex equivalents. ( ) → 3.0 (/ 2 3.0) → (+ #c(1 2) 3) → #c(4 2)(+ #c(1 2) 3/2) → #c(5/2 2) (+ #c(1 1) #c(2 -1)) → 3 9

BASIC MATH Because / doesn’t truncate, Common Lisp provides four flavors of truncating and rounding for converting a real number to an integer: FLOOR truncates toward negative infinity, returning the largest integer less than or equal to the argument. CEILING truncates toward positive infinity, returning the smallest integer grea ter than or equal to the argument. TRUNCATE truncates toward zero, making it equivalent to FLOOR for positive arguments and to CEILING for negative arguments. And ROUND rounds to the nearest integer. If the argument is exactly halfway between two integers, it rounds to the nearest even integer. 10 > (floor 30 4) 7 ; 2 > (ceiling 30 4) 8 ; -2 > (round 30 4) 8 ; -2 > (round 29 4) 7 ; 1 > (truncate 30 4) 7 ; 2 > (truncate 29 4) 7 ; 1

BASIC MATH Two related functions are MOD and REM, which return the modulus and remainder of a truncating division on real numbers. These two functions are related to the FLOOR and TRUNCATE functions as follows: (+ (* (floor (/ x y)) y) (mod x y)) ≡ x (+ (* (truncate (/ x y)) y) (rem x y)) ≡ x 11 > (floor 29 4) 7 ; 1 > (mod 29 4) 1 > (truncate 29 4) 7 ; 1 > (rem 29 4) 1

BASIC MATH The functions 1+ and 1- provide a shorthand way to express adding and subtracting one from a number. Note that these are different from the macros INCF and DECF. 1+ and 1- are just functions that return a new value, but INCF and DECF modify a place. The following equivalences show the relation between INCF/DECF, 1+/1-, and +/-: (incf x) ≡ (setf x (1+ x)) ≡ (setf x (+ x 1)) (decf x) ≡ (setf x (1- x)) ≡ (setf x (- x 1)) (incf x 10) ≡ (setf x (+ x 10)) (decf x 10) ≡ (setf x (- x 10)) 12

NUMERIC COMPARISONS The function = is the numeric equality predicate. It compares numbers by mathematical value, ignoring differences in type. (= 1 1) → T (= 10 20/2) → T (= #c( ) #c(1 0)) → T The /= function returns true only if all its arguments are different values. (/= 1 1) → NIL (/= 1 2) → T (/= 1 2 3) → T (/= ) → NIL (/= ) → NIL 13

NUMERIC COMPARISONS The functions, = order rationals and floating-point numbers. ( 2 3) → NIL (> 3 2) → T(< 2 3 4) → T (< 2 3 3) → NIL(<= 2 3 3) → T (<= ) → T(<= ) → NIL To pick out the smallest or largest of several numbers, we can use the function MIN or MAX, which takes any number of real number arguments and returns the minimum or maximum value. (max 10 11) → 11 (min ) → -12 (max ) → 6 14

NUMERIC COMPARISONS Some other functions are ZEROP, MINUSP, and PLUSP, which test whether a single real number is equal to, less than, or greater than zero. Two other predicates, EVENP and ODDP, test whether a single integer argument is even or odd. The P suffix on the names of these functions is a standard naming convention for predicate functions, functions that test some condition and return a boolean. Lisp also supports logarithms: LOG; exponentiation: EXP and EXPT; the basic trigonometric functions: SIN, COS, and TAN; their inverses: ASIN, ACOS, and ATAN; hyperbolic functions: SINH, COSH, and TANH; and their inverses: ASINH, ACOSH, and ATANH. It also provides functions to get at the individual bits of an integer and to extract the parts of a ratio or a complex number. 15

EXERCISE Write a function NUMCOM which can read two positive number, and calculate all the prime numbers between their sum and product values. For example: >(numcom) Please input two positive numbers: 2 7 The prime numbers between 9 and 14 are 11 and 13. Do you want to try again? Y Please input two positive numbers: is not a positive number. Do you want to try again? Y Please input two positive numbers: a 7 a is not a number. Do you want to try again? N NIL 16

CHARACTERS Common Lisp characters are a distinct type of object from numbers. The read syntax for characters objects is simple: #\ followed by the desired character. Thus, #\x is the character x. Any character can be used after the #\, including otherwise special characters such as ", (, and whitespace. We can write #\Space instead of #\, though the latter is technically legal. Other semistandard names are Tab, Page, Rubout, Linefeed( 換行 ), Return, and Backspace. 17

CHARACTER COMPARISONS The main thing you can do with characters, other than putting them into strings, is to compare them with other characters. For example: > (char= #\e #\E) NIL > (char-equal #\e #\E) T 18

STRINGS Literal strings are written enclosed in double quotes. We can include any character supported by the character set in a literal string except double quote (") and backslash (\). For examples: > "foo\"bar" "foo\"bar" 19 > (format t "foo\"bar") foo"bar NIL

STRING COMPARISONS We can compare strings using a set of functions that follow the same naming convention as the character comparison functions except with STRING as the prefix rather than CHAR. For example: > (string= "THIS" "this") NIL 20 > (string-equal "THIS" "this") T

STRING COMPARISONS The string comparators can compare two substrings. The arguments—:start1, :end1, :start2, and :end2—specify the starting (inclusive) and ending (exclusive) indices of substrings in the first and second string arguments. For example, (string= "foobarbaz" "quuxbarfoo" :start1 3 :end1 6 :start2 4 :end2 7) compares the substring “bar” in the two arguments and returns true. The :end1 and :end2 arguments can be NIL (or the keyword argument omitted altogether) to indicate that the corresponding substring extends to the end of the string. For examples: Break 10 [11]> (string= "foobarbaz" "quuxbarfoo" :start1 3 :start2 4 ) NIL Break 10 [11]> (string= "foobar" "quuxbar" :start1 3 :start2 4 ) T 21

STRING COMPARISONS The comparators that return true when their arguments differ. All of them except STRING= and STRING-EQUAL return the index in the first string where the mismatch was detected. (string/= "lisp" "lissome") → 3 (string/= "lisp" "lisp") → NIL If the first string is a prefix of the second, the return value will be the length of the first string. (string< "lisp" "lisper") → 4 (string< "lisper" "lisp") → NIL When comparing substrings, the resulting value is still an index into the string as a whole. For instance, the following compares the substrings “bar” and “baz” but returns 5 because that’s the index of the r in the first string: (string< "foobar" "abaz" :start1 3 :start2 1) → 5 ;not 2 22

EXERCISE Please write a function STRCOM, which can read two input strings STR1 and STR2 and check if STR1 is a substring of STR2 or STR2 is a substring of STR1. > (STRCOM) Please input the first string STR1: abcdefghijk Please input the second string STR2: cdefg STR2 is a substring of STR1 from position 2 to position 6. Do you want to check other strings? Y Please input the first string STR1: aaaa Please input the second string STR2: cccaaaaaaccc STR1 is a substring of STR2 from position 3 to position 6. Do you want to check other strings? Y Please input the first string STR1: abcd Please input the second string STR2: ccaacc STR1 is not a substring of STR2 and STR2 is not a substring of STR1. Do you want to check other strings? N NIL PS. Access substrings: 23