Question

In: Computer Science

What is an S-expression? What is a cons cell? What is an atom? A pair? A list?

What is an S-expression? 

What is a cons cell? 

What is an atom? A pair? A list? 

What do car and cdr do? 

What do the map and reduce functions do? 

Write a recursive function to compute the length of a list. 

Write a function to compute the length of a list using reduce.

Solutions

Expert Solution

what is an S-expression?

S-expression, also known as sexpr or sexp. It is a way to represent a nested list of data. It stands for symbolic expression and it is used in Lisp programming language. The  variants of Lisp such as Scheme, Racket, and Clojure. These programming languages use s-expressions to represent the computer program, as well as the program's data.

Example: The simple mathematical expression "six times the sum of four and two" can be written as a s-expression with prefix notation. In Lisp, the s-expression might look like the example below.

(* 6 (+ 4 2))

what is a cons cell?

Lists in the Lisp language are not primitive data type, they are built from cons cell. Cons cell are data objects represents the ordered pair, it has two slots and each slot holds or refer to some Lisp object. One slot is car and the other one is cdr. (These names are traditional) cdr is "could-er".

"the car of this cons cell is" whatever object its car slot currently holds, and like wise for the cdr.

List is a series of cons cells chained together, so the each cell refers to the next one. There are one cons cell for each element of the list. In convention, cars of the cons cell hold the elements of the list and cdrs are used to chain the list(this asymmetry between car and cdr is entirely matter of convention. the level of cons cells, the car and cdr slots have similar properties). Thus the cdr slot of each cons cell in list refers to the following cons cell.

what is a atom?A pair? A list?

The definition of an atom varies per context, it is assumed that there exists, An infinite set of distinguishable atomic symbols can be represented as strings of capital Latin letters and digits with single embedded blanks.(character string and numeric literals). Most modern sexpr notation in addition use an abbreviated notation to represent lists in s-expression, so that

(a b c)

stands for

(a. (b. (c .NIL)))

where NIL is the special end-of-list object.

Pair joins two arbitrary values. Cons procedure constructs pairs, car and cdr procedures extract the first and second elements of the pair.

List is combination of pairs that creates a linked list. List is either the empty list null, or it is pair whose first element is a list element and second element is a list.

what do car and cdr do?

Cons cell are data objects represents the ordered pair, it has two slots and each slot holds or refer to some Lisp object. One slot is car and the other one is cdr. (These names are traditional) cdr is "could-er".

"the car of this cons cell is" whatever object its car slot currently holds, and like wise for the cdr.

List is a series of cons cells chained together, so the each cell refers to the next one. There are one cons cell for each element of the list. In convention, cars of the cons cell hold the elements of the list and cdrs are used to chain the list(this asymmetry between car and cdr is entirely matter of convention. the level of cons cells, the car and cdr slots have similar properties). Thus the cdr slot of each cons cell in list refers to the following cons cell.

what do the map and reduce functions do?

Map function takes set of data and converts into another set of data, where individual elements are broken down into tuples(key/value pairs)

Reduce function task, takes the output from the map as an input and combines those data tuples into smaller set of tuples.

write a recursive function to compute the length of a list?

def lislength(l):
     if l:
         return 1 + lislength(l[1:])
     return 0
write a function to compute the length of a list using reduce?

import reduce # reduce() is built-in in Python 2.

def list_length(L):
    return reduce(lambda x,y: x+1, L, 0)

Related Solutions

lambda calculus: what does the following expression evaluate to? fst (snd (fst (pair (pair (pair 2...
lambda calculus: what does the following expression evaluate to? fst (snd (fst (pair (pair (pair 2 3) (pair 4 5)) (pair (pair 6 7) (pair 8 9))) ))
for each of the following pair of endocrine disorders, list the gland(s) and hormone(s) involved and...
for each of the following pair of endocrine disorders, list the gland(s) and hormone(s) involved and then indicate one difference between the disorders a.Diabetes mullitus and diabetes insipidus b. cushing syndrome and Addison diease c. Hypothyroidism and hyperthyroidism
In stressful conditions, a cell wants to limit expression of Protein X. List all the ways...
In stressful conditions, a cell wants to limit expression of Protein X. List all the ways that this can occur.
In stressful conditions, a cell wants to limit expression of Protein X. List all the ways...
In stressful conditions, a cell wants to limit expression of Protein X. List all the ways that this can occur.
Given a list of fruits, fruits = ["apple", "pear", "banana", "cherry" ] which expression(s) will return...
Given a list of fruits, fruits = ["apple", "pear", "banana", "cherry" ] which expression(s) will return the last two items in fruits in this order : cherry, banana? a) fruits[-3:-1] b) fruits[-1:-len(fruits) + 2:-1] c) fruits[-1:-3:-1] d) fruits[:-3:-1] Which Python variable names are invalid? a) 12MonthsTotal_Pay b) TotalPay$ c) Total_January2019 d) JanuaryRateln% what is the output of executing the code segment? a, b, c = False, True, False if a or b: print('A', end = ' ') else: print('B', end...
suppose that a bf3 molecule approaches the lone pair on the N atom of an ammonia...
suppose that a bf3 molecule approaches the lone pair on the N atom of an ammonia molecule (:NH3) and that a bond forms between the B atom and the N atom. Consider the arrangement of electron pairs about the B atom at the moment of this bond formation and describe the repulsions among the electron pairs and how they might be expected to change the geometry around the boron atom. Use Lewis structures to illustrate your explanation.
What are the pros and cons of the Linux OS. List the following in your answer:...
What are the pros and cons of the Linux OS. List the following in your answer: Brief history of the Linux OS Different Linux OS versions What is open source verses closed source In what ways is Linux better than Windows? What are the GUI Shell? What are the features of each one and which OS do they belong to?
List the quantum numbers for the electrons in the ground state of a neon atom. What...
List the quantum numbers for the electrons in the ground state of a neon atom. What are the quantum numbers for the next state to be filled in this scheme? If a single electron were excited by the minimum amount of energy possible, what would then be the configuration of the atom?
2. Calculation of the atom economy of the reaction according to the following expression (8): %...
2. Calculation of the atom economy of the reaction according to the following expression (8): % Atom Economy = (Mol. Wt. of Product / S(Mol. Wt. of Reactants)) x 100 How might the atom economy be improved by changing the structure of one of the starting materials? Can you predict any drawbacks to doing this?
explain the expression: G-T=S-I what it is how it is obtained
explain the expression: G-T=S-I what it is how it is obtained
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT