0

For example, we need to implement function Set addToSet(Stack, Object): it accept a Stack and some object that can be pushed into the stack; the input object should be pushed into the input stack; we need it to return a Set, which contains all elements of the input stack with the new object. If we push the new object into the stack, we violate the principle of functional programming. For some reason, we can't push the new object into the stack before or after invoking this function (i.e. do pushing outside the function). How should we do?

My guess solution is to copy the input stack to a new one first, and then modify the new stack and finally return the result Set with the new stack together. What's your opinion?

象嘉道
  • 3,657
  • 5
  • 33
  • 49
  • Why the heck return a **set**, instead of a stack? The two are completely different and incompatible data structures. –  Oct 21 '12 at 15:29
  • @delnan This is a simple example: I have a much more complicated version of this question, but nobody will answer it if post it. I am leaning FP :) – 象嘉道 Oct 21 '12 at 15:33
  • 1
    I mean, if you're operating on a stack but ultimately want a set, that's fine. But then it would make much more sense to have two separate functions: One to add an element on a stack (returning another stack), and one to convert a stack into a set. I get that this is not your whole problem, but it's so astonishingly pointless to fuse the two operations that I can't help but assume you're not decomposing your problem properly. –  Oct 21 '12 at 15:34
  • @delnan very useful ideas hitting me! thanks a lot. the original version is here (*appending* part): http://stackoverflow.com/questions/12993979/pure-functional-programming-how-to-add-an-element-to-a-list . – 象嘉道 Oct 21 '12 at 15:50

1 Answers1

1

How about:

function Stack pushToStack(Stack, Object)
function Set stackToSet(Stack)
stackToSet(pushToStack(Stack,Object))

No more side effect.

Need4Steed
  • 2,170
  • 3
  • 22
  • 30
  • Well, assuming `pushToStack` has no side effects, which doesn't appear to be obvious to OP. –  Oct 21 '12 at 15:37
  • Pure functional programming doesn't allow any modification(state changing), a function can't change a existing object, it just discard the old one and return the new one. So either you bend the rule or do something like I wrote above. You feed the `pushToStack` a Stack A and an Object O, then your get a new Stack A', then feed it to `stackToSet`, you get a new Set S. That's all! – Need4Steed Oct 21 '12 at 15:42
  • Yes, it just does what you guessed. `call pushToStack(origianl_stack,obj) => ( get a new stack with the object at the top) then call stackToSet(bigger_stack) => (get the set converted from the bigger_stack)` If the language you use supports multiple return values that won't be a problem, you can just merge the two functions to one which return them both. But such a language won't be called functional. The function `addToSet(stk,obj)` functionally speaking is actuall a combination of addToSet(stk) => return a curried function `f'`, then call the curried function `f'` with obj as argument. – Need4Steed Oct 21 '12 at 15:59