Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:54:55+00:00 2026-06-09T03:54:55+00:00

In another thread The best way to construct a function with memory , it

  • 0

In another thread The best way to construct a function with memory, it was described how to back in file a function:

$runningLogFile = "/some/directory/runningLog.txt";
flog[x_, y_] := flog[x, y] = f[x, y] /.
v_ :> (PutAppend[Unevaluated[flog[x, y] = v;], $runningLogFile]; v)

I feel like I understand most of the ingredients here without understanding exactly how this works. Any chance someone could walk me through exactly how this is evaluated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:54:57+00:00Added an answer on June 9, 2026 at 3:54 am

    Let’s walk through the evaluation of flog[1, 2], step-by-step…

    flog[1, 2]

    When this expression is evaluated, Mathematica will substitute 1 for x and 2 for y in the definition of flog given in the question. This yields the next step in our tour:

    flog[1, 2] =
    f[1, 2] /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile];
    v)

    Note carefully that the assignment here, flog[1, 2] = ..., is part of the definition of flog itself.

    /. is an infix operator that is an alternate representation of the ReplaceAll function. ReplaceAll will apply a replacement rule to the value of the first argument. Hold that thought — we’ll come back to it. The first argument is flog[1, 2] = f[1, 2]. This expression will evaluate f[1, 2] and then assign the result to flog[1, 2]. For the sake of discussion, let’s assume that f[1, 2] returns 345. Thus, a new definition will be added to flog, namely flog[1, 2] = 345. After assignment, we can check the definition of flog:

    flog definition

    Observe that where flog only had a single definition initially but now it has two — the newly added flog[1, 2] definition caching the result of that call. This is frequently called “memoization”.

    flog[1, 2] = 345 may have the side-effect of establishing a new definition for flog but, like every expression in Mathematica, it yields a value as well. The value is 345 which, after much ado, will be the first argument to ReplaceAll.

    The second argument to ReplaceAll is an invocation of the :> operator, an infix expression of the RuleDelayed function. In an effort to keep this post to a manageable size, we’ll simply note that the rule evaluates to itself in this context.

    So, now we have an expression that involves /. to evaluate…

    345 /. v_ :> (PutAppend[Unevaluated[flog[1, 2] = v;], $runningLogFile]; v

    A replacement expression matches its first argument (345) with the pattern component of the replacement rule (v_). v_ matches 345 (or anything else for that matter) and gives 345 the name v for purposes of replacement. ReplaceAll then substitutes 345 for every occurrence of v in the right hand side of the rule. The result is the next expression to be evaluated…

    (PutAppend[Unevaluated[flog[1, 2] = 345;], $runningLogFile]; 345)

    Here we have two expressions separated by a semicolon. Incidentally, ; is an infix operator that expands to CompoundExpression. The first expression involves PutAppend which writes the value of its first argument to the file named as the value of the second argument. Note, however, that the first argument is wrapped in Unevaluated. This suppresses the evaluation of the first argument so that it will be written exactly as-is to the file: flog[1, 2] = 345;. Should the current Mathematica session end, the written expression can be read into a future Mathematica session to re-establish the memoized result for flog[1, 2].

    CompoundExpression discards the value of all arguments except the last. Here, the last argument is 345. Since we have come to the end of our expression, this will be the final return value of the original call. That is, flog[1, 2] returns 345 — although as we saw there were side-effects that saved this result to memory and disk for future reference.

    Future calls to flog[1, 2]

    Now if flog[1, 2] is called again, Mathematica will find the new definition flog[1, 2] = 345. 345 will be returned directly, without any of the complications that we discussed above. In particular, it won’t even call f[1, 2] again. This, of course, was the whole motivation for this example. The assumption was that f was very expensive to calculate, justifying all of these gymnastics to minimize the number of times that calculation occurs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the recommended / best way to implement a blocking function call in
what is the best way to stop a thread and wait for a statement
Answering another thread (see stackoverflow: generate css color schemes ) I bumped into the
[Note: There is another thread about this problem but it did not answer the
I originally started this question in another thread, but that thread was sorta, kinda
I want to access a scrollviewer from another thread. Please tell me how to
I am tring to setText in another thread, that is, child thread. But for
As I don't want to hijack another thread here comes my question about mappings.
I want to update a Object in another thread, and then access it in
When one thread manages an std::map adding and removing things, may another thread safely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.