2
$\begingroup$

I am going over The Wolfram Language:Fast introduction to programmers. In the associations section, following question and answer is given

Which of the following applies a template to make a string with appetizer and dessert from the association

 meal=<|"appetizer" →"nachos", "salad" → "spinach", "dessert" → "chocolate"|>
 TemplateApply["The appetizer is `appetizer` and the dessert is `dessert`.", meal]

But in my mathematica notebook, I am getting errors from the above two lines of code.

Incompatible elements in Join .... cannot be joined.

But according to web site, this is correct answer. Where is my mistake?

According @N0va, this is a typo. See below screenshot.

Demo of Typo

$\endgroup$
3
  • 1
    $\begingroup$ One of the most unfortunate and hard to detect Typos I have seen on this page: Somehow the arrows in meal are wrong while being displayed correctly. Using this should work meal = <|"appetizer" -> "nachos", "salad" -> "spinach", "dessert" -> "chocolate"|>;. $\endgroup$ – N0va Oct 23 '20 at 12:20
  • $\begingroup$ That is correct, with your comment it works. Please enter it as answer so that I can accept it $\endgroup$ – Atilla Ozgur Oct 23 '20 at 12:33
  • $\begingroup$ But this is an stopping error for beginners. Both outputs are hard to distinguish for beginners. $\endgroup$ – Atilla Ozgur Oct 23 '20 at 12:41
4
$\begingroup$

To make a short story short: use -> for the Association:

mealWorking=<|"appetizer"->"nachos","salad"->"spinach","dessert"->"chocolate"|>;
TemplateApply["The appetizer is `appetizer` and the dessert is `dessert`.",meal]

generates the desired output:

The appetizer is nachos and the dessert is chocolate.

To make a short story long and to elaborate a bit on my comment on OPs question: This is one of the worst (in the sense of hard to detect) and unfortunate typos I have seen. The problem can not be fixed when looking at the InputForm

meal=<|"appetizer" →"nachos", "salad" → "spinach", "dessert" → "chocolate"|>

only. The output

Association["appetizer" \[RightArrow] "nachos", "salad" \[RightArrow] "spinach", "dessert" \[RightArrow] "chocolate"]

however shows the problem: this is not a valid Association but the pasted text here already spoils the conclusion which is not so obvious inside a notebook:

Output

The arrows in the definition are \[RightArrow] and they should be \[Rule] (equivalent to ->) so the solution is

mealWorking = <|"appetizer" -> "nachos", "salad" -> "spinach",  "dessert" -> "chocolate"|>

looking like this in the notebook

wokring

Hard to detect since inside a notebook Mathematica displays \[Rule] and \[RightArrow] using its Wolfram System fonts which do not distinguish between the two symbols. One way to detect the difference is looking at the cell expression (select the input cell an use the menu item Cell->Show Cell Expression or its shortcut) to display

Cell expression

or copy the plain text to an editor outside of Mathematica. To be honest I think Mathematica should indicate things like this as syntax errors but sadly it does not.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.