"HeldExpression" (Comparison Method)
Details
- The "HeldExpression" comparison method considers two expressions to be equivalent if they are the same within any holding wrappers.
- Both answer key values and submitted answers are typically provided with a holding wrapper such as Hold, HoldPattern or HoldComplete.
- Answer key values containing patterns must be given using HoldPattern[expr] to support standard pattern matching. Using HoldComplete or Hold requires literal matching of expressions.
Examples
open all close allBasic Examples (2)
Create an AssessmentFunction for an unevaluated expression:
heldtest = AssessmentFunction[Hold[Nest[f, x, 3]], "HeldExpression"]The submitted answer must exactly match:
heldtest[Hold[Nest[f, x, 3]]]The evaluated result is not marked as correct:
heldtest[Hold[f[f[f[x]]]]]Create an AssessmentFunction for an unevaluated mathematical expression:
heldmath = AssessmentFunction[Hold[1 + 2], "HeldExpression"]The submitted answer must be identical:
heldmath[Hold[1 + 2]]Mathematically equivalent submissions that are not identical are marked incorrect:
heldmath[Hold[2 + 1]]heldmath[Hold[3]]Scope (1)
Create an assessment function for a code question:
rangetest = AssessmentFunction[Hold[Range /@ Range[4]], "HeldExpression"]Use it in a QuestionObject:
qo = QuestionObject[QuestionInterface["Code", "Use only Range and Map to create this output:
{{1},{1,2},{1,2,3},{1,2,3,4}}"],
rangetest]qoAttempting to submit the output will be marked as incorrect:
qoProperties & Relations (4)
Assessment functions using Hold will treat patterns literally:
holdassess = AssessmentFunction[Hold[Nest[f, x, _]], "HeldExpression"]Submissions that match the pattern will be marked as incorrect:
holdassess[Hold[Nest[f, x, 2]]]Exact literal matches are marked as correct:
holdassess[Hold[Nest[f, x, _]]]Use HoldPattern instead to support pattern matching:
holdpatternassess = AssessmentFunction[HoldPattern[Nest[f, x, _]], "HeldExpression"]Matching held expressions are marked as correct:
holdpatternassess[Hold[Nest[f, x, 2]]]The evaluated result is not accepted:
holdpatternassess[Hold[f[f[x]]]]The "Expression" comparison method compares the full expression, including holding wrappers when they exist:
exprcomp = AssessmentFunction[HoldPattern[_ + 2], "Expression"]Different holding wrappers will not be marked as correct:
exprcomp[Hold[1 + 2]]Use "HeldExpression" to compare the expression within the holding wrapper:
heldexprcomp = AssessmentFunction[HoldPattern[_ + 2], "HeldExpression"]The same answer is marked correct:
heldexprcomp[Hold[1 + 2]]Create assessment for a held mathematical expression:
holdaf = AssessmentFunction[Hold[1 + 2 + 3], "HeldExpression"]Create another assessment for treating the same answer as an "ArithmeticResult":
mathaf = AssessmentFunction[Hold[1 + 2 + 3], "ArithmeticResult"]"HeldExpression" does not allow even basic equivalences like reordering terms in addition, but "ArithmeticResult" does:
holdaf[Hold[3 + 1 + 2]]mathaf[Hold[3 + 1 + 2]]Create assessment for a held code expression:
holdaf = AssessmentFunction[HoldPattern[Table[i ^ 2, {i, RandomInteger[10]}]], "HeldExpression"]Create another assessment for treating the same answer as a "CodeEquivalence" solution:
codeaf = AssessmentFunction[HoldPattern[Table[i ^ 2, {i, RandomInteger[10]}]], "CodeEquivalence"]"HeldExpression" does not attempt to find equivalences between non-identical answers, but "CodeEquivalence" does:
holdaf[HoldPattern[# ^ 2& /@ Range[RandomInteger[10]]]]codeaf[HoldPattern[# ^ 2& /@ Range[RandomInteger[10]]]]Possible Issues (1)
QuestionObject allows formatting of values even when given in Hold wrappers:
QuestionObject["Which creates a 2D display of values", AssessmentFunction[{Hold[Grid[{{a, b}, {c, d}}]] -> 1, Hold[Table[{{a, b}, {c, d}}, 1]], Hold[{Row@{a, b}, Row@{c, d}}]}, "HeldExpression"]]Use DisableFormatting to prevent formatting in QuestionObject:
QuestionObject["Which creates a 2D display of values", AssessmentFunction[{Hold[DisableFormatting@Grid[{{a, b}, {c, d}}]] -> 1, Hold[Table[{{a, b}, {c, d}}, 1]], Hold[DisableFormatting@{Row@{a, b}, Row@{c, d}}]}, "HeldExpression"]]See Also
AssessmentFunction QuestionObject Hold HoldPattern MatchQ
Question Interface Types: ShortAnswer Code
Comparison Methods: Expression CodeEquivalence ArithmeticResult