Build, rewrite and evaluate R expressions with quote, substitute and eval

Quick Overview

Write four base R functions that build sum and weighted-sum calls, replace symbols in a template by walking its call tree, and evaluate the result against a list of data. It tests R metaprogramming with call, as.name, Reduce and eval, plus safe environment scoping.

Build, rewrite and evaluate R expressions with quote, substitute and eval

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement four base R functions that build, rewrite and evaluate unevaluated language objects (not strings) using tools like `quote`, `bquote`, `call`, `as.name` and `eval`. Results must be usable directly by `eval`. - `build_sum_expr(vars)` returns a left-associative sum call (`c("a", "b", "c")` gives `a + b + c`). A single name returns the bare symbol, and length 0 calls `stop()`. - `build_weighted_expr(weights, vars)` returns a left-associative weighted sum such as `2 * x + 3 * y`. A single pair returns the product call, and differing or zero lengths call `stop()`. - `substitute_expr(template, values)` replaces every symbol named in the list `values` (whose entries are numbers or language objects), recursing through nested calls and leaving unmatched symbols intact. It must walk the call tree explicitly and must not rely on `substitute()` promise semantics. - `eval_expr(expr, data)` evaluates `expr` in an environment built from the named list `data` and returns the numeric scalar or vector. "Left-associative" means `a + b + c` is `(a + b) + c`, which matches the call tree R itself builds for `+`. Example: ```r e <- build_sum_expr(c("a", "b", "c")) # a + b + c build_weighted_expr(c(2, 3), c("x", "y")) # 2 * x + 3 * y tmpl <- quote(a + b) substitute_expr(tmpl, list(a = 10)) # 10 + b eval_expr(quote(2 * x + 3 * y), list(x = 1, y = 2)) # 8 ``` ```hint Folding from the left Write `a + b + c` as nested calls: `` `+`(`+`(a, b), c) ``. Which base function folds a list from the left with a starting value, and what should that starting value be? ``` ```hint A call behaves like a list A call object can be indexed and modified with `[[`, and its first element is the function. Decide what your walker does with a symbol, with a call, and with anything else. ``` ### Constraints & Assumptions - Base R only: no external packages. - `vars` is a character vector of variable names and `weights` is a numeric vector. - Every result is a language object (a call or a symbol), never a string to be parsed. - `substitute_expr` may not call `substitute()`, directly or through `do.call`. ### What a Strong Answer Covers - Building calls with `call()`, `as.name()` or `bquote()` and folding from the left so the tree matches R's own parse of `a + b + c`. - Handling the single-element, empty and mismatched-length cases exactly as specified. - A recursive walk that treats symbols, calls and constants separately and rebuilds each call from its transformed elements. - Evaluating in an environment built from `data` with a deliberately chosen parent, and why that choice matters. - Checking results with `identical()` against `quote(...)` and by evaluating them. ### Follow-up Questions - Why does the built-in `substitute()` often not do what you want here? - How would you evaluate an expression without leaking into the global scope? - Your walker also visits the function position of every call. When is that a bug, and how would you skip it? - Why is building language objects safer than pasting strings together and calling `parse()`?

Overview: Write four base R functions that build sum and weighted-sum calls, replace symbols in a template by walking its call tree, and evaluate the result against a list of data. It tests R metaprogramming with call, as.name, Reduce and eval, plus safe environment scoping.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement four base R functions that build, rewrite and evaluate unevaluated language objects (not strings) using tools like quote, bquote, call, as.name and eval. Results must be usable directly by eval.

  • build_sum_expr(vars) returns a left-associative sum call ( c("a", "b", "c") gives a + b + c ). A single name returns the bare symbol, and length 0 calls stop() .
  • build_weighted_expr(weights, vars) returns a left-associative weighted sum such as 2 * x + 3 * y . A single pair returns the product call, and differing or zero lengths call stop() .
  • substitute_expr(template, values) replaces every symbol named in the list values (whose entries are numbers or language objects), recursing through nested calls and leaving unmatched symbols intact. It must walk the call tree explicitly and must not rely on substitute() promise semantics.
  • eval_expr(expr, data) evaluates expr in an environment built from the named list data and returns the numeric scalar or vector.

"Left-associative" means a + b + c is (a + b) + c, which matches the call tree R itself builds for +.

Example:

e <- build_sum_expr(c("a", "b", "c"))   # a + b + c
build_weighted_expr(c(2, 3), c("x", "y"))  # 2 * x + 3 * y

tmpl <- quote(a + b)
substitute_expr(tmpl, list(a = 10))       # 10 + b

eval_expr(quote(2 * x + 3 * y), list(x = 1, y = 2))  # 8

Constraints & Assumptions

  • Base R only: no external packages.
  • vars is a character vector of variable names and weights is a numeric vector.
  • Every result is a language object (a call or a symbol), never a string to be parsed.
  • substitute_expr may not call substitute() , directly or through do.call .

What a Strong Answer Covers Guidance

  • Building calls with call() , as.name() or bquote() and folding from the left so the tree matches R's own parse of a + b + c .
  • Handling the single-element, empty and mismatched-length cases exactly as specified.
  • A recursive walk that treats symbols, calls and constants separately and rebuilds each call from its transformed elements.
  • Evaluating in an environment built from data with a deliberately chosen parent, and why that choice matters.
  • Checking results with identical() against quote(...) and by evaluating them.

Follow-up Questions Guidance

  • Why does the built-in substitute() often not do what you want here?
  • How would you evaluate an expression without leaking into the global scope?
  • Your walker also visits the function position of every call. When is that a bug, and how would you skip it?
  • Why is building language objects safer than pasting strings together and calling parse() ?