- When
f(x-1)is called, is it callingf(x) = x+10orf(x) = if ... - Is this a tail recursion?
How should I rewrite it using static / dynamic allocation?
let fun f(x) = x + 10 in let fun f(x) = if x < 1 then 0 else f(x-1) in f(3) end end
several questions about this sml recursion function
79 Views Asked by AudioBubble At
1
There are 1 best solutions below
Related Questions in RECURSION
- What is the problem in my "sumAtBis" code?
- Leetcode 1255-recursion and backtracking
- Unexpected Recursive Call
- Clang possibly skipping line(s) of code while compiling
- Return an arraylist without passing an argument
- Solving Maze using Backtracking C++
- I can't get the specific node of BST using recursion . i.e. every stack it erase
- Python Quadtree won't insert values
- Top View Of Binary Tree Depth First Search Using TreeMap
- Select/filter tree structure in postgres
- Python global variables in recursion get different result
- Trying to recursively find the area of a polygon
- *Dynamically* decorate a recursive function in Python
- What structure can be made to avoid having to use RefCell?
- Why is the output of the two given cout statements different in the given cpp code
Related Questions in DYNAMIC-MEMORY-ALLOCATION
- Variable doesn't get updated when I run a loop
- C Changing value of array of struct through reference
- What is wrong with this Reflection.Emit for value conversion delegates?
- What are the differences between using vector and using new,delete in c++?
- Find the some smallest negative using Dynamic memory allocation in C
- Access violation after reallocating memory
- Why using a pointer of a self-defined structure in the CS50 Inheritance problem
- How to read the amount of memory block consumed by pointer?
- Return string (or char *) from a C function
- How to define different [global_allocator]s for a monolithic os kernel and its applications
- why can i not free ctypes memory in c?
- Problem with reallocating array during runtime in Cpp
- Valgrind showing invalid read of size 4 when using an erase function
- Byte array and int transferring between C# and C++ in Unity DLL Integration
- How to check if the requested memory chunk can be allocated?
Related Questions in SML
- Creating an instance of a Binary Tree (Programming Standard ML by Robert Harper)
- Obtain a function as tree in SML
- TextIO.openIn:"No such file or directory" in Poly/ML
- How to implement Label/TextVIew with giraffe library in SML
- Compiling Giraffe library Hello World on Ubuntu
- How to match function type in signature in SML
- Error "no *.cm, *.mlb, or millet.toml files found in this directory" in VS code using SML
- I'm new to SML/NJ and there's some questions bothering me. I don't know how to use Math or String in the basis lib
- SML Looping through 2 Random variables and ordering them?
- How do I fix my standard ml code when I have an unbound value 'a'?
- How to install and run SML/ NJ in VScode
- UTF-8, Unicode in SML/NJ
- What is the difference between ('a,'b) and ('a*'b)?
- sml standard pop function vs user defiend. And return values
- Error: unbound type constructor: TypeInteger
Related Questions in TAIL-RECURSION
- TailRec optimisation and export
- Is `Pair` a valid instance of `MonadRec`?
- Improving efficiency in Stirling numbers calculation
- How can I create a tail recursive merge method in Scala on a self-referential tree structure (or is it even possible)?
- How to optimize this algorithm using tail recursion?
- XSLT recursion crashes after 1000 calls - how to transform it to DVC style?
- Stack overflow when composing functions in F#
- How are my recursive calls not tail calls here?
- How to collect maximal non-overlapping ascending/descending prefixes of a random sequence of numbers
- Append a list on to another in oCaml
- Recursion in a tailored map function - don't understand the program flow
- Confused about how tail recursion works?
- Tail call optimization in Clojure
- Performance of deep recursion versus tail recursion
- Identifying Tail-Recursion
Related Questions in STATIC-ALLOCATION
- C++ templates: choice between static and dynamic allocations (a la Eigen)
- Persistent config with visitor pattern and static memory allocation
- Segmentation fault in DAG
- Reinitialize dynamically allocated memory
- Returning a pointer to a static buffer
- Hacker rank questions on finding the odd number
- Best statically allocated data structure for writing and extending contiguous blocks of data?
- Synchronizing Statically Allocated Struct Instances between CPU and GPU
- How are allocated arrays declared in a loop?
- several questions about this sml recursion function
- Deallocating locally defined variables in C
- embedded c++ : dynamic typing without dynamic allocation?
- Is it possible to create class String without using heap in C++?
- static allocation and stack allocation in compiler design
- Replace dynamic allocation with a static one
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Before addressing your questions, here are some observations about your code:
There are two functions
f, one inside the other. They're different from one another.To lessen this confusion you can rename the inner function to
g:This clears up which function calls which by the following rules: The outer
fis defined inside the outerin-end, but is immediately shadowed by the innerf. So any reference tofon the right-hand side of the innerfun f(x) = if ...is shadowed becausefunenables self-recursion. And any reference tofwithin the innerin-endis shadowedIn the following tangential example the right-hand side of an inner declaration
fdoes not shadow the outerfif we were usingvalrather thanfun:If the inner
fis renamed togin this second piece of code, it'd look like:The important bit is that the
f(x + 2)part was not rewritten intog(x + 2)becausevalmeans that references tofare outerfs, not thefbeing defined, because avalis not a self-recursive definition. So any reference to anfwithin that definition would have to depend on it being available in the outer scope.But the
g(3)bit is rewritten because betweenin-end, the innerf(nowg) is shadowing. So whether it's afunor avaldoes not matter with respect to the shadowing oflet-in-end.(There are some more details wrt.
val recand the exact scope of alet val f = ...that I haven't elaborated on.)As for your questions,
You should be able to answer this now. A nice way to provide the answer is 1) rename the inner function for clarity, 2) evaluate the code by hand using substitution (one rewrite per line,
~>denoting a rewrite, so I don't mean an SML operator here).Here's an example of how it'd look with my second example (not your code):
Your evaluation by hand would look different and possibly conclude differently.
What is tail recursion? Provide a definition and ask if your code satisfies that definition.