Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 (A) Write Scheme code that creates the following: [3 marks] A cons pa

ID: 3601869 • Letter: Q

Question

Question 1

(A) Write Scheme code that creates the following: [3 marks]

A cons pair of two numbers

A list of 3 numbers, using only the cons function.

A list containing a string, a number and a nested list of three numbers, using only the cons function.

A list containing a string, a number and a nested list of three numbers, using only the list function.

A list containing a string, a number and a nested list of three numbers, using only the append function.

(B) Comment on result. Mention the difference between the cons, list and append functions. [3 marks]

Explanation / Answer

#| this is used for block commment in Scheme language |#

#| Cons is a list construction function used to construct a list
by taking arguments which can be either elements or a list |#

#| Cons takes two arguments - the first one can be an element or a list
the second argument is generally considered a list
All Cons does is to append the first one to the second one |#

#| There's a special case where the second argument can be just an element.
In that case, Cons makes it a dotted pair |#

#| To answer the first question, create a cons pair of two numbers |#
(cons 1 2)
#| It prints (1 . 2) A Dotted pair |#

#| If you want a normal pair |#
(cons 1 '(2))
#| It prints (1 2) |#

#| To answer the second question, create a list of 3 numbers using Cons |#
(cons 1 (cons 2 '(3)))
#| It prints (1 2 3) A list of three numbers |#

#| To answer the third question, create a list of a string,a number and
a nested list of 3 numbers using Cons |#
(cons "James" (cons 0 (cons (cons 1 (cons 2 '(3))) ())))
#| It prints the desired list |#

#| list function simply returns a list consisting of its arguments |#

#| To answer the fourth question, create a list of a string,a number and
a nested list of 3 numbers using list function|#
(list "Bond" 7 (list 1 2 4))
#| It prints the desired list |#

#| append function concatenates all its argument lists |#

#| To answer the fifth question, create a list of a string,a number and
a nested list of 3 numbers using append function|#
(append '("Harry") '(7) '((1 2 4)))
#| It prints the desired list |#

#| To answer the fifth question, Mention the difference between the cons, list and append functions
- Append function requires its arguents to be pure lists, but any number of lists are accepted
- Cons function requires that there be only 2 arguents of any type
- List function can take any no of any type of arguents
- Append concatenates the argument lists. It only concatenates all the elements of the toplevel lists, it doesn't "flatten" nested lists
- List simply keeps all of its arguments inside a list and returns. It also does not flatten
- Cons just tries to insert its first argument into the second argument-list. But in case of second argument being an element, it creates a dotted pair with first argument and that element

Examples :
(append '(1 2) '(3 4))
returns a list (1 2 3 4).

Notice that this is different from what list does:

(list '(1 2) '(3 4)
returns ((1 2) (3 4))

which is again different from cons which does:
cons '(1 2) '(3 4))
=> ((1 2) 3 4)

|#

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote