Group Abstract Group Abstract

Message Boards Message Boards

29
|
116K Views
|
22 Replies
|
120 Total Likes
View groups...
Share
Share this post:

[CALL] Most common pitfalls for beginners of Wolfram Language

Wolfram Language (WL) is a powerful multi-paradigm programing language. There is a set of common mistakes that repeatedly tend to entrap new users. This is a call to describe such mistakes building a "black-listing" guide for novice coders. Please consider contributing. I suggest following simple rules (with gratitude adapted from a similar effort):

  • One topic per answer

  • Focus on non-advanced uses (it is intended to be useful for beginners and as a question closing reference)

  • Include a self explanatory title in header style (example: "# Basic built-in function syntax"; see syntax guide )

  • Explain the symptoms, the mechanism behind the scenes and all possible causes and solutions you can think of. Be sure to include a beginner's level explanation (and a more advance one too, if you can)

Please, use "Reply" to a specific comment for structured clarity of nested comments.


Table of Contents

POSTED BY: Vitaliy Kaurov
22 Replies
Posted 4 years ago
POSTED BY: Mark Bourland
Posted 4 years ago
POSTED BY: Pedro Cabral
POSTED BY: Jon McLoone

If i would get a euro for every time I see horrible/bad approach…

POSTED BY: Sander Huisman

Subscripted variables are a pain...use [i] indexing instead.

POSTED BY: Yaroslav Bulatov
Attachments:
POSTED BY: Raspi Rascal
Posted 9 years ago

Import and "CurrencyTokens"

Perhaps not 'common' but I was bitten by it twice until I made it 'my' default for data import. I assume for some legacy reason the stripping of currency symbols is the default behavior of the Import[] function. Seems an odd default behavior to me and worse the default behavior is not pointed out as boldly as I think it should be in the documentation.

Import[   , "CurrencyTokens" -> None]

Incorrect Import

Correct Import

POSTED BY: David Proffer

Numerical vs Symbolic -- What to do when plots are blank (and other strange errors)

The plotting functions require numerical inputs. This sounds obvious but most people have trouble debugging plots that come out empty. When something does not plot, the first thing to do is to evaluate the expression and look for undefined variables or typos.

Consider the following:

A = 12;
b = 3;
expr = a* b sin[theta];
Plot[expr, {theta, 0, 10}]

which yields a less than informative plot:

enter image description here

The best approach is to take the plot argument and evaluate it at a point somewhere in the plot range and see that it is numerical. It is often a good idea to evaluate it at the two extremes and the middle to make sure it is numerical everywhere.

expr /. theta -> 0
expr /. theta -> 5
expr /. theta -> 10

to get

 (* 3 a sin[0] 
3 a sin[5] 
3 a sin[10] *)

It becomes clear that the a is undefined and that sin does not evaluate (because of the lower case 's')

POSTED BY: Neil Singer

Case sensitivity and typos

I am far from a novice user, but I make a lot of typing mistakes, so case sensitivity catches me often...especially on properties and names in WDF, etc.

This command gets an error

ResourceData["On the origin of Species"]

This command gets the data

ResourceData["On the Origin of Species"]

This command gets an error...also I find the error message to be somewhat opaque

DateObject[{2016, 8, 4}, "week"]

This command evaluates the object

DateObject[{2016, 8, 4}, "Week"]
POSTED BY: Aeyoss Antelope

Consider Reap/Sow Instead of AppendTo

When using the output of a program to build large lists, a combination of Reap and Sow is often cited as the most efficient (computationally speaking) approach. Put simply, AppendTo trades speed in favour of flexibility by creating a copy of the original list when called. Indeed, Reap/Sow is also point 7 of 10 Tips For Writing Fast Mathematica Code.

I remember first seeing Reap/Sow as a beginner and feeling like it was an 'advanced' feature for other users. Instead I fell into the pattern of defining an empty list and using AppendTo which in hindsight was a beginner's mistake I frequently made by the justification given above.

The example from the link above says it best:

In[7]:= data = {}; 
 Do[AppendTo[data, RandomReal[x]], {x, 0, 40000}]; // AbsoluteTiming

Out[7]= {11.6127, Null}

In[8]:= data = 
   Reap[Do[Sow[RandomReal[x]], {x, 0, 40000}]][[2]]; // AbsoluteTiming

Out[8]= {0.164533, Null}

This is one small change which will Reap large benefits for any beginner.

POSTED BY: Benjamin Goodman
POSTED BY: Michael Rogers
POSTED BY: Sander Huisman
POSTED BY: Raspi Rascal

Thanks, had added a previous version of ApplyTo, now fixed, and also added [Application]. (https://reference.wolfram.com/language/ref/Application.html)

POSTED BY: Sander Huisman

see "x|->f — new syntax for Function with named variables"

btw your post is the only bookmark I am keeping from this community site.

POSTED BY: Raspi Rascal

Also added this explicitly. thanks!

POSTED BY: Sander Huisman

For some reason the following code is not working for me: ?5.5?=Missing["UnknownSymbol", "5.5?"] Has the floor function syntax been updated?

POSTED BY: Peter Burbery
Posted 4 years ago
POSTED BY: Rohit Namjoshi

The forum screwed up some of the symbols and replaced them with ?. Now fixed.

POSTED BY: Sander Huisman
POSTED BY: Sander Huisman
POSTED BY: Patrick Scheibe

Basic syntax of built-in functions

  • All built-in functions start from capital letters and are in CamelCase for compound names. Users of many other programming languages might miss this as they are used to different conventions. Examples: Plot, ListPlot, FindSpanningTree, etc.

  • Arguments of a function are inclosed in square brackets. Round parenthesis are used only for ordering of operations. Again, other languages use different conventions. Examples:

    • Cos[Pi] --- is a function with a single argument Pi.
    • BesselJ[1/2, 5] --- is a function with 2 arguments
    • (2-Cos[Pi])Sin[Pi/3] --- round parenthesis are used to order operations
POSTED BY: Marina Shchitova
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard