Skip to main content
added 2 characters in body
Source Link
user56834
  • 4.3k
  • 5
  • 22
  • 36

I'm not asking this question for the purpose of any particular project. Rather, I'm trying to understand how to translate non-trivial programs in imperative style to functional style. By functional program/style I mean one in which all objects are immutable and functions are pure, and so forth.

The only example I've seen so far of translating imperative code to functional code is for highly specific and simple codeThe only example I've seen so far of translating imperative code to functional code is for highly specific and simple code. In particular, the example that seems to be used a lot is the factorial function, and translating iteration into recursiontranslating iteration into recursion:

//imperative -- iteration int factorial (n) for i=1;i<n;i++ x=x*i; end for return x; //functional -- recursion int factorial (n) if (n==1) return 1; else return factorial (n-1) * n 

However, this is such a specific example of a function (the factorial), that it doesn't make it clear to me how in general to translate core constructs in imperative style to functional style. e.g. can we always translate a for loop into recursion?

I'm looking for a comprehensive textbook or other resource that in generality shows how to translate imperative code to functional code

  • How do we translate the basic building blocks of imperative code to functional code? Can we even do this in general?

  • How do we translate some examples of not-completely-trivial (e.g. not the factorial function) programs?

  • AreEspecially interesting, are there fully general automated algorithms for doing these translations? Are they actually used? I can imagine that some compilers do something like this.

  • (perhaps also interesting, the opposite, translating functional code to imperative code.)

I'm not asking this question for the purpose of any particular project. Rather, I'm trying to understand how to translate non-trivial programs in imperative style to functional style. By functional program/style I mean one in which all objects are immutable and functions are pure, and so forth.

The only example I've seen so far of translating imperative code to functional code is for highly specific and simple code. In particular, the example that seems to be used a lot is the factorial function, and translating iteration into recursion:

//imperative -- iteration int factorial (n) for i=1;i<n;i++ x=x*i; end for return x; //functional -- recursion int factorial (n) if (n==1) return 1; else return factorial (n-1) * n 

However, this is such a specific example of a function (the factorial), that it doesn't make it clear to me how in general to translate core constructs in imperative style to functional style. e.g. can we always translate a for loop into recursion?

I'm looking for a comprehensive textbook or other resource that in generality shows how to translate imperative code to functional code

  • How do we translate the basic building blocks of imperative code to functional code? Can we even do this in general?

  • How do we translate some examples of not-completely-trivial (e.g. not the factorial function) programs?

  • Are there fully general automated algorithms for doing these translations? Are they actually used?

I'm not asking this question for the purpose of any particular project. Rather, I'm trying to understand how to translate non-trivial programs in imperative style to functional style. By functional program/style I mean one in which all objects are immutable and functions are pure, and so forth.

The only example I've seen so far of translating imperative code to functional code is for highly specific and simple code. In particular, the example that seems to be used a lot is the factorial function, and translating iteration into recursion:

//imperative -- iteration int factorial (n) for i=1;i<n;i++ x=x*i; end for return x; //functional -- recursion int factorial (n) if (n==1) return 1; else return factorial (n-1) * n 

However, this is such a specific example of a function (the factorial), that it doesn't make it clear to me how in general to translate core constructs in imperative style to functional style. e.g. can we always translate a for loop into recursion?

I'm looking for a comprehensive textbook or other resource that in generality shows how to translate imperative code to functional code

  • How do we translate the basic building blocks of imperative code to functional code? Can we even do this in general?

  • How do we translate some examples of not-completely-trivial (e.g. not the factorial function) programs?

  • Especially interesting, are there fully general automated algorithms for doing these translations? Are they actually used? I can imagine that some compilers do something like this.

  • (perhaps also interesting, the opposite, translating functional code to imperative code.)

Source Link
user56834
  • 4.3k
  • 5
  • 22
  • 36

resource on translating imperative programs to functional programs

I'm not asking this question for the purpose of any particular project. Rather, I'm trying to understand how to translate non-trivial programs in imperative style to functional style. By functional program/style I mean one in which all objects are immutable and functions are pure, and so forth.

The only example I've seen so far of translating imperative code to functional code is for highly specific and simple code. In particular, the example that seems to be used a lot is the factorial function, and translating iteration into recursion:

//imperative -- iteration int factorial (n) for i=1;i<n;i++ x=x*i; end for return x; //functional -- recursion int factorial (n) if (n==1) return 1; else return factorial (n-1) * n 

However, this is such a specific example of a function (the factorial), that it doesn't make it clear to me how in general to translate core constructs in imperative style to functional style. e.g. can we always translate a for loop into recursion?

I'm looking for a comprehensive textbook or other resource that in generality shows how to translate imperative code to functional code

  • How do we translate the basic building blocks of imperative code to functional code? Can we even do this in general?

  • How do we translate some examples of not-completely-trivial (e.g. not the factorial function) programs?

  • Are there fully general automated algorithms for doing these translations? Are they actually used?