Message Boards Message Boards

0
|
3280 Views
|
7 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Combine the result/information given by Trace/Hold/FullForm gracefully

Posted 2 years ago

IMHO, the Trace, Hold, and FullForm are three commands of Wolfram language to supply valuable debug/inspection information for a complicated expression. For example, the following command can be used to obtain the real path from the opened notebook itself:

"FileName" /. NotebookInformation[] /. _[{path__}, fileName_, __] :> 
  FileNameJoin@{path, fileName}

In order to understand the implications and working logic of the above code snippet, I try to run the following two commands in the same notebook:

"FileName" /. NotebookInformation[] /. _[{path__}, fileName_, __] :> 
   FileNameJoin@{path, fileName} // Trace
"FileName" /. NotebookInformation[] /. _[{path__}, fileName_, __] :> 
    FileNameJoin@{path, fileName} // Hold // FullForm

But the first command gives a lot of output information, while the second command gives an expression with many levels of nesting. All in all, they're very unintuitive. More specifically, if I can tweak, tame, and combine all the information into a more intuitive and readable output format, their practical value will be greatly improved.

I wonder if anyone has this experience. Any tips/hints will be highly appreciated.

Regards,
HZ

Attachments:
POSTED BY: Hongyi Zhao
7 Replies
Posted 2 years ago

Thank you for your advice. I'll check and read the book recommended above.

POSTED BY: Hongyi Zhao
Posted 2 years ago

Hi Alexey Popkov,

Thank you for your comment.

Why do you use the following code snippet in your explanation:

h[path, fileName]

instead of the following form:

{path, fileName}

In fact, the following two commands will give exactly the same results:

NotebookFileName[EvaluationNotebook[]]
NotebookFileName[]

But what I want to tackle here is a general problem, and the command above is only used as an example. More specifically, I want to have a customized function which can combine the outputs of the commands mentioned above to give more elegant and readable results.

POSTED BY: Hongyi Zhao
Posted 2 years ago

Why do you use the following code snippet in your explanation

Just to illustrate the point of destructuring more clearly: the FullForm of {path, fileName} is List[path, fileName], and h[path, fileName] is just a generalization of this. It empasizes the point that you don't have to use the Head List, you can use any Head.

I want to have a customized function which can combine the outputs of the commands mentioned above to give more elegant and readable results.

You need to learn some basics about pattern matching and writing functions in Mathematica.

POSTED BY: Alexey Popkov
Posted 2 years ago

Just to illustrate the point of destructuring more clearly: the FullForm of {path, fileName} is List[path, fileName], and h[path, fileName] is just a generalization of this. It empasizes the point that you don't have to use the Head List, you can use any Head.

Things like this are very difficult to understand for me, at least for now. For the sake of clarity, let me express my confusion with the following example:

In[49]:= Clear[a, x, y, b]
{a, List[x, y], b}
{a, {x, y}, b}
{a, Sequence[x, y], b}
{a, h[x, y], b}

Out[50]= {a, {x, y}, b}

Out[51]= {a, {x, y}, b}

Out[52]= {a, x, y, b}

Out[53]= {a, h[x, y], b}

As you can see, after the destructuring, I got three different results.

POSTED BY: Hongyi Zhao
Posted 2 years ago

You are just not familiar with some very basic programming concepts. I recommend learning them.

For Mathematica, I recommend the book by Leonid Shifrin as a quick and clear introduction:

https://www.mathprogramming-intro.org/

POSTED BY: Alexey Popkov
Posted 2 years ago

About the code snippet you are trying to understand. I don't think that Trace can help you in this case. Probably the most difficult part is the pattern _[{path__}, fileName_, __]. This pattern performs a destructuring of the supplied expression based on knowing the exact structure of the latter.

The supplied expression returned by "FileName" /. NotebookInformation[] can look like:

expr = FrontEnd`FileName[{$RootDirectory, "C:", "Users", "APopkov", "Desktop"}, "myNotebook.nb", CharacterEncoding -> "UTF-8"]

Check what the pattern does:

expr /. _[{path__}, fileName_, __] :> h[path, fileName]
h["", "C:", "Users", "APopkov", "Desktop", "myNotebook.nb"]

I hope that starting from this point you can reconst the whole logic yourself.

POSTED BY: Alexey Popkov
Posted 2 years ago

Actually there is NotebookFileName for obtaining the full path to an opened Notebook:

NotebookFileName[EvaluationNotebook[]]
POSTED BY: Alexey Popkov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract