DOC PREVIEW
UA CSC 520 - Symbols and Structures

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSc 520 — Principles of Programming Languages37 : Scheme — Symbols and StructuresChristian CollbergDepartment of Computer ScienceUniversity of [email protected] 2008 Christian CollbergMay 2, 20081 Symbols• In addition to numbers, strings, and booleans, Scheme has a primitive data-type (atom) called symbol.• A symbol is a lot like a string. It is written:’identifier• Here are some examples:’apple’pear’automobile• (symbol? arg) checks if an atom is a symbol.• To compare two symbols for equality, use (eq? arg1 arg2). HTDP says to use (symbol=? arg1arg2) but DrScheme doesn’t seem to support this.12 Symbols. . .> (symbol? "hello")#f> (symbol? ’apple)#t> (eq? ’a ’a)#t> (eq? ’a ’b)#f> (display ’apple)apple> (string->symbol "apple")apple> (symbol->string ’apple)"apple"3 Symbols. . .(define (healthy? f)(case f[(sushi sashimi) ’hell-yeah][(coke) ’I-wish][(licorice) ’no-but-yummy][else ’nope]))> (healthy? ’sashimi)hell-yeah> (healthy? ’coke)i-wish> (healthy? ’licorice)no-but-yummy> (healthy? ’pepsi)nope4 Structures• Some versions of Scheme have structures. Select Advanced Student in DrScheme.• These are similar to C’s struct, and Java’s class (but without inheritance and methods).• Use define-struct to define a structure:(define-struct struct-name(f1 f2 ...))• define-struct will automatically define a constructor:(make-struct-name(f1 f2 ...))and field-selectors:2struct-name-f1struct-name-f25 Structures. . .(define-struct person (name sex date-of-birth))> (define bob (make-person "bob" ’male ’1978))> bob(make-person "bob" ’male ’1978)> (define alice (make-person "alice" ’female ’1979))> (person-sex bob)’male> (person-date-of-birth alice)’19796 Equivalence• Every language definition has to struggle with equivalence; i.e. what does it mean for two languageelements to be the same?• In Java, consider the following example:void M( S t r i n g s1 , S t r i n g s2 , int i 1 , i n t i 2 ) {i f ( i 1 == i2 ) . . . ;i f ( s1 == s2 ) . . . ;i f ( s1 . e q u a l s ( s2 ) ) . . . ;}Why can I use == to compare ints, but it is it usually wrong to use it to compare strings?7 Equivalence. . .• Scheme has three equivalence predicates eq?, eqv? and equal?.• eq? is the pickiest of the three, then comes eqv?, and last equal?.• In other words,– If (equal? a b) returns #t, then so will (eq? a b) and (eqv? a b).– If (eqv? a b) returns #t, then so will (eq? a b)..• (equal? a b) generally returns #t if a and b are structurally the same, i.e. print the same.8 Equivalence. . .(eqv? a b) returns #t if:• a and b are both #t or both #f.3• a and b are both symbols with the same name.• a and b are both the same number.• a and b are strings that denote the same locations in the store.> (define S "hello")> (eqv? S S)true> (eqv? "hello" "hello")false> (eqv? ’hello ’hello)true9 Equivalence. . .• (equal? a b) returns #t if a and b are strings that print the same.• This is known as structural equivalence.> (equal? "hello" "hello")true> (equal? alice bob)false> (define alice1 (make-person "alice" ’female ’1979))> (define alice2 (make-person "alice" ’female ’1979))> (equal? alice1


View Full Document

UA CSC 520 - Symbols and Structures

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

Scheme

Scheme

9 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Symbols and Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Symbols and Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Symbols and Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?