Docsity

Introduction to Computer Science: Abstraction, Syntax, Semantics, and Racket Programming, Summaries of Computer Science

Partial preview of the text

Download Introduction to Computer Science: Abstraction, Syntax, Semantics, and Racket Programming and more Summaries Computer Science in PDF only on Docsity!

Module 01: Introduction

Introduction to Computer Science 1

University of Waterloo

Fall 2023

xkcd.com/297/

Computer science is not programming …But we will use programming as a concrete medium in which to explore the nature of computation. "Computer science is no more about computers than astronomy is about telescopes." Edsger Dijkstra (?)

Racket

  • A^ pure functional^ programming language
  • Stripped-down syntax
  • Unfamiliar to most students taking the course
  • Really good for teaching and learning

The Language of Mathematics

The Language of Mathematics We all learned to write in a non-native language called Mathematics, whether we thought about it that way or not.

- Values:^ ,^ ,^ ,^ ,^ … - Expressions: , , , … - Definitions: , , … - Evaluation rules: 0 − 37 3.14159 7 / 8 (6,4) 5 + 3 log 2 ( 2 ⋅ 8 ) 5 9 ( 90 − 32 ) 2 100 − 5 a = 13 π = 3.14159… f ( x ) = x 2 − x − 1 f ( a − 1 ) =?

The Language of Mathematics We all learned to write in a non-native language called Mathematics, whether we thought about it that way or not.

- Values:^ ,^ ,^ ,^ ,^ … - Expressions: , , , … - Definitions: , , … - Evaluation rules: 0 − 37 3.14159 7 / 8 (6,4) 5 + 3 log 2 ( 2 ⋅ 8 ) 5 9 ( 90 − 32 ) 2 100 − 5 a = 13 π = 3.14159… f ( x ) = x 2 − x − 1 f ( a − 1 ) = 131

More Terminology

a = 13

f ( x ) = x

− x − 1

f ( a − 1 )

Identifier

More Terminology

a = 13

f ( x ) = x

− x − 1

f ( a − 1 )

Parameter

Argument

Body

Built-In Racket Functions

  • In Mathematics,^ denotes the absolute value function:^ , , etc.
  • Racket has a built-in function called^ abs; but how do we apply it? | ⋅ | | 4 | = 4 | − 8 | = 8

Function Application Syntax Every Racket function application follows the same pattern:

  • An^ open parenthesis^ (
  • An^ identifier^ (the name of the function)
  • Some^ whitespace^ (spaces, tabs, newlines)
  • An^ argument^ (an expression producing a value passed to the function)
  • Any additional arguments, separated by more whitespace
  • A^ closing parenthesis^ )
  • In Mathematics, we use operator precedence (PEMDAS, BODMAS, etc.) and parentheses to resolve ambiguity 5 ⋅ 6 + 7 ≠ 5 ⋅ ( 6 + 7 ) 5 ⋅ 6 + 7 = ( 5 ⋅ 6 ) + 7
  • Racket's syntax is unambiguous by design. There are no precedence rules, and extra parentheses are never needed (in fact, they're illegal!) 5 ⋅ 6 + 7 (+ (* 5 6 ) 7 )

Useful Math Functions

  • add1,^ sub
  • quotient,^ remainder
  • abs
  • sqr,^ sqrt
  • expt See the Racket documentation for more!

Constants

  • Math contains a number of agreed-upon constants:^ ,^ , etc.
  • These are useful in programming, and therefore built in to Racket:^ pi,^ e.
  • Named constants:^ identifiers that are permanently bound to values
  • Of course, in math we can invent new constants as needed:
  • Can we do something similar in Racket? π e G = 6.6743 × 10

Constant Definitions

  • We use the^ define^ special form^ to create new constants in Racket: (define cs115-grade 100 ) (define current-inflation-rate 7.6) (define avogadro 6.02214e23)
  • Once defined, these behave like any other named constants.