Short Quiz 3 (

advertisement
Short Quiz 3
1.
Is atom data and list implemented using same data structure ?
( Yes / No )
2. CAR - returns the first element of a list. Examples:
• (CAR ‘(a b c d e))
• (CAR ‘((an orange) a day))
• (CAR '(1 (2 3 (4 5)) 6))
• (CAR ‘())
Note that in the last example, the argument to CAR is a null-list.
Then, if (CAAR(‘l)) = (CAR(CAR(‘l))) , what is the result of
(CAAR ‘((1 2 ) (3 4)) )
1
3. Note that the result of a CAR need not be an atom but that CAR is only
designed to take arguments which are lists, not atoms.
CDR [pronounced "could-er"] is the complement of CAR in that the result
of CDR is the "rest" of the list:
(CDR '(1 2 3 4)) = ( 2 3 4 )
Then, what is the result of ( CADR ‘(1 2 3 ) )
2
4. A symbol naming a variable. In other words, (setf x y) is exactly
equivalent to (setq x y), and setq itself is strictly speaking redundant now
that setf exists. Many programmers continue to prefer setq for setting simple
variables, though, purely for stylistic or historical reasons. The macro (setf x
y) actually expands to (setq x y), so there is no performance penalty for
using it in compiled code. Example:
(setq my-name "David")
"David"
Then, what is the result of (setq a (list "hello" "world")) ?
("hello" "world")
5. Knowledge Interchange Format (KIF) is a computer-oriented language
for the interchange of knowledge among disparate programs. Examples:
(or (numberp 'c-major) (symbolp 'd-major))
T
The predicates NOT and NULL return the same results. NULL is
generally used to check for the empty list and NOT is used to reverse
a T or NIL evaluation.
(not (= 4 5))
T
What is result of (or (numberp 'c-major) (symbolp 'd-major))
T
6.
(defun f (N) "Take input of N."
(if (= N 1)
1
(* N (f (- N 1)))))
This is a function you might have seen a lot of times, guess what it is ?
And please write down corresponding C / pseudo code program.
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Take an Input of N\n");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
7. FORMAT function takes two required arguments: a destination for its
output and a control string that contains literal text and embedded directives.
Any additional arguments provide the values used by the directives in the
control string that interpolate values into the output. I'll refer to these
arguments as format arguments.
The first argument to FORMAT, the destination for the output, can
be T, NIL, a stream, or a string with a fill pointer. T is shorthand for the
stream *STANDARD-OUTPUT*,while NIL causes FORMAT to generate
its output to a string, which it then returns. If the destination is a stream, the
output is written to the stream. And if the destination is a string with a fill
pointer, the formatted output is added to the end of the string and the fill
pointer is adjusted appropriately. Except when the destination is NIL and it
returns a string, FORMAT returns NIL.
Please give result of
(format nil "foo") => "foo"
(setq x 5)
(format nil "The answer is ~D." x) => “The answer is 5.”
8. In Scheme, where do you think the symbol-lookup-table and valuelookup-table are placed? (Stack / Heap / Static memory )
9. Please draw a parse tree to show how following LISP sentence is parsed:
( t1 (t2 (t3 6
(t4 9 2 ) ) 4
t5( 3 1 ) ) 7)
t1 t2 7 t3 t4 6 9 t5 4 2 3 1 10. This is a Scheme expression that evaluates to my-favorite student's letter
grade in CSE305:
Solutions: This is a choice and so maps onto a Scheme if expression:
(if (>= my-favorite 0.90)
'A
(if (>= my-favorite 0.80)
'B
(if (>= my-favorite 0.70)
'C
(if (>= my-favorite 0.60)
'D
'F))))
Please try to do it using ‘switch’ statement cond to implement it:
> (cond ((>= my-favorite 0.90) 'A)
((>= my-favorite 0.80) 'B)
((>= my-favorite 0.70) 'C)
((>= my-favorite 0.60) 'D)
(else 'F))
Download