Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.2K Views
|
7 Replies
|
1 Total Like
View groups...
Share
Share this post:

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

Posted 3 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 3 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 3 years ago

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

POSTED BY: Hongyi Zhao
Posted 3 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 3 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 3 years ago
POSTED BY: Hongyi Zhao
Posted 3 years ago
POSTED BY: Alexey Popkov
Posted 3 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