Scheme Flashcards
What is the car of I where l is ((a b c) x y z)
(a b c), because (a bc ) is the first S-expression of this non-empty list.
What is (car (car I)) where I is (((hotdogs) (and))
(hotdogs).
What is the cdr of I where I is ((a b c) x y z)
(x y z).
What is the cdr of I where l is (hamburger)
().
What is (cdr I) where l is ((x)tr)
(t r),
because (cdr I) is just another way to ask for “the cdr of the list I.”
The Law of Cdr
The primitive cdr is defined only for non-empty lists. The cdr of any non- empty list is always another list.
What does car take as an argument?
It takes any non-empty list.
What does cdr take as an argument?
It takes any non-empty list.
What is the cons of the atom a and the list I where a is peanut andI is (butter and jelly)
This can also be written “(cons a I)”.
Read: “cons the atom a onto the list I.”
(peanut butter and jelly),
because cons adds an atom to the front of a list.
What does cons take as its arguments?
cons takes two arguments:
the first one is any S-expression;
the second one is any list.
The Law of Cons
The primitive cons takes two arguments. The second argument to cons must be a list. The result is a list.
What is (null? (quote ()))
True,
because (quote ()) is a notation for the null list.
The Law of Null?
The primitive null? is defined only for lists.
True or false: a lat is a list of atoms.
True! Every lat is a list of atoms!
Í Scheme viljum við forrita án hliðarverkana, hvað þýðir það?
Það þýðir að við viljum ekki nota neinar gildisveitingar. Breytur fá því gildi þegar þær verða til og fá aldrei nýtt gildi.
Hvað þurfum við að gera til þess að forrita án hliðarverkana í Scheme?
Við notum endurkvæmni (recursion) í stað lykkju.
Hvað gerir endurkvæmt fall?
Kallar á sjálft sig
Hvað gerir hala endurkvæmt fall ?
Endurkvæmt fall sem endar á að kalla á sjálft sig (eða jafnvel annað endurkvæmt fall) er kallað halaendurkvæmt (tail recursive).
Afhverju er halaendurkvæmni hagstæð í Scheme ?
Þegar Scheme fall endar á að kalla á annað fall (eða sjálft sig) er strax hætt í núverandi falli og næsta fall tekur við og skilar sínu gildi til þess sem kallaði á upphaflega fallið.
Listavinnsla í Scheme byggist á föllunum?
car, cdr og cons
Hvað gerir car í Scheme ?
Gefur þér fyrsta stak í lista
Hvað gerir cdr í Scheme ?
Gefur þér allt sem kemur á eftir fyrsta stakinu
Hvernig myndi ég fá stak 2 í lista?
Með því að kalla á car af cdr af listanum, eða cadr sem er short fyrir það
Hvað gerir cons?
Setur stak fremst í lista