Symbolic Programming with LISP Quiz Quiz

Explore essential aspects of symbolic programming in LISP through practical scenarios and knowledge checks. This quiz covers fundamental constructs, list processing, recursion, and the distinctive features of LISP relevant to symbolic computation.

  1. Understanding LISP Data Structures

    In LISP, which data structure is primarily used for representing both code and data, exemplified by the expression (add 2 3)?

    1. Arrays
    2. Lists
    3. Maps
    4. Stacks

    Explanation: LISP uses lists to represent both code and data, which allows for flexible symbolic manipulation. Stacks are used for managing function calls but are not the primary data representation. Arrays exist in LISP but are less central to symbolic programming. Maps are a concept in other languages and do not form the core representation in LISP's symbolic paradigm.

  2. Recursion in Symbolic Processing

    Which of the following best illustrates a recursive function definition in LISP that counts elements in a list, such as (count-elements '(a b c))?

    1. (defun count-elements (lst) (car lst))
    2. (defun count-elements (lst) (if (null lst) 0 (+ 1 (count-elements (cdr lst)))))
    3. (defun count-elements (lst) (append lst nil))
    4. (defun count-elements (lst) (setf lst (cdr lst)))

    Explanation: The first option demonstrates a recursive approach, checking for an empty list and otherwise calling itself with the rest of the list. The second option only retrieves the first element, not counting elements. The third option merely appends the list and does not perform counting, while the fourth option modifies the input but does not count or recurse.

  3. Key Characteristics of LISP Functions

    Which feature distinguishes all LISP functions, allowing them to be treated as data and passed to other functions, as seen in higher-order constructs like 'mapcar'?

    1. Static typing
    2. Pointer arithmetic
    3. Strict evaluation
    4. First-class functions

    Explanation: First-class functions in LISP allow functions to be passed as arguments, returned from other functions, and assigned to variables. Static typing does not apply in LISP, which is dynamically typed. Strict evaluation describes evaluation strategy but is not unique to LISP functions. Pointer arithmetic is a low-level operation not directly associated with LISP programming.

  4. Symbol Manipulation and Evaluation

    Given the expression (quote (+ 2 2)) in LISP, what is returned and why?

    1. 4
    2. (+ 2 2)
    3. '+4
    4. Error: Invalid syntax

    Explanation: The quote prevents the evaluation of the expression and returns the list structure as written: (+ 2 2). Without quote, LISP would evaluate the sum and return 4. The syntax is valid, so there is no error. The '+4 option is not a standard output for quoted LISP expressions.

  5. List Processing with 'car' and 'cdr'

    If xs is assigned the list (a b c d), what does the LISP expression (cdr xs) return?

    1. (a b c d)
    2. (d c b)
    3. (b c d)
    4. a

    Explanation: The cdr function returns the tail of the list, omitting the first element, resulting in (b c d). Option 'a' would be the result of car, not cdr. The option '(a b c d)' is the original list, and '(d c b)' reverses the order incorrectly, which is not the behavior of cdr.