2
\$\begingroup\$

If your solution for a question requires, say, two functions, that should be called one after another, is it acceptable to define the two functions, with it being expected that the user will invoke the two in sequence?

That is, if you define two functions f(x) and g(x), with the intention being that the user would call them as f(g(x)), is this an acceptable solution, or does the solution need to have a h(x)=f(g(x)) definition or equivalent, so that the user would simply call h(x)?

What is the community's position on this?

\$\endgroup\$
13
\$\begingroup\$

Single function call

The answer must provide a single function to call, i.e. the h(x)=f(g(x)) case, otherwise it would outsource the function composition f(g(x)) to the caller. It also prevents misusing this mechanism, e.g.

Task: take the 4th element of a list. Haskell answer: head;tail, call it like head.tail.tail.tail

\$\endgroup\$
  • \$\begingroup\$ Those aren't defining functions, though, they're calling functions. \$\endgroup\$ – Glen O Nov 8 '15 at 11:20
  • \$\begingroup\$ To me, "write a program or function" means write a self-contained function that requires only a single call. g(x) can be defined and called within f(x) or h(x) but it can't be defined outside of it. \$\endgroup\$ – Alex A. Nov 8 '15 at 22:00
  • \$\begingroup\$ @AlexA. Ummm... I think it's common practice to do so. I don't think we have any rules against function submissions using global state (as long as they don't break themselves), and defining a function g outside of f or h is just a global function that is being called. \$\endgroup\$ – Martin Ender Nov 10 '15 at 9:10
  • \$\begingroup\$ @MartinBüttner Oh, okay. I didn't realize that. \$\endgroup\$ – Alex A. Nov 10 '15 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .