Introduction to S-Expressions and Lists in Atom

Classified in Physics

Written at on English with a size of 3.92 KB.

  • Atom is pretty much just a string of characters with no spaces
  • Atom --> atom
  • Atom123 --> atom
  • ( ) --> NOT an atom, it’s actually a list

All atoms are S-expressions

A list is enclosed in parenthesis

All lists are S-expressions too

  • (how are you doing) --> List
  • (atom) --> a list because its an atom enclosed by parentheses
  • ( ) --> List

The car of an argument is the first S-Expression in something

  • The car of j where j is ((a b c) x y z) = (a b c)
  • The car of j where j is hotdog doesn’t exist because you can’t ask for the car of an atom
  • The (car (car j)) where j is (((hotdogs)) (and)) = (hotdogs)

The cdr of an argument is anything that follows the car of an argument

  • The cdr of j where j is (a b c) = (b c)
  • The cdr of j where j is ((a b c) x y z) = (x y z)

cons adds S-expressions to the front of lists ***

  • If a is peanut and j is (butter and jelly), (cons a j) = (peanut butter and jelly)
  • If s is (a b c)) and l is ( ), then (cons a l) = ((a b c))) //because ( ) is a list technically

null? can be asked only on lists and will return true if the list is empty

atom? asks if an s-expression is an atom and returns true if it is

  • Takes an S-Expression as its argument

eq? takes two non-numeric atoms as arguments and returns true if they are equal

  • (eq? a1 a2) where a1 is Harry and a2 is Harry would return true

lat? function asks if every S-Expression in a list is an atom, and returns true if every S-Expression is an atom

(define lat?
  (lambda (l)
   (cond
    ((null? l) !=t
    ((atom?) (car l)) (lat? (cdr l)))
    (else !=f))))

member? function takes an atom and a list of atoms (lat) and returns true if the atom is a member of the lat

(define member?
  (lambda (a lat)
   (cond
    ((null? lat) #f )
    (else (or (eq? (car lat) a)
     (member? a (cdr lat)))))))

rember takes an atom and a lat as its arguments, and makes a new lat with the first occurrence of the atom in the old lat removed

(define rember
  (lambda (a lat)
   (cond
    ((null? lat) (quote()))
    (else (cond
     ((eq? (car lat) a) (cdr lat))
     (else (cons (car lat)
      (rember a (cdr lat))))))))

firsts takes one argument, a list, which is either a null list or contains only non-empty lists. It builds another list composed of the first S-expression of each internal list

insertR (insertR new old lat) takes three arguments: the atoms new and old , and a lat. The function insertR builds a lat with new inserted to the right of the first occurrence of old

(define insertR
  (lambda (new old lat)
   (cond
    ((null? lat) (quote()))
    (else (cond
     ((eq? (car lat) old)
      (cons old
       (cons new (cdr lat))))
     (else (cons (car lat)
      (insertR new old
       (cdr lat))))))))))

Entradas relacionadas: