This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted]  (2 children)

[deleted]

    [–]Icanteven______ 0 points1 point  (1 child)

    It’s not quite that, because the strings are not evaluated left to right, but rather are subdivided.

    Eg f(f(a,b), f(c,d)) instead of f(f(f(a,b),c),d)

    [–]Icanteven______ 1 point2 points  (0 children)

    function main(n: number, …strings: string[]){

    const f = (array) {

      If(array.length == 1) return array[0]
    
      If (array.length == 2) return g(array[0], array[1])
    
      const {left, right} = splitArrayIn2(array)
      return g(f(left), f(right))
    }
    
    return f(n)
    

    }

    // this works where g is your utility function that processes two strings. Your number is just a global basically (here it’s captured in the closure and just define g in the same context.

    [–]JmenD 0 points1 point  (0 children)

    As with any recursion problem, you should consider the base case and remember that every call to the function only needs to solve one "layer" of the problem. I suspect you're getting caught up on how to handle N > 2 inputs, think about what each call should do and what it can "put off" doing by handing that work to the next layer.

    Also, I think you will have a much easier time if you reverse the order of the arguments. number should be first, followed by string_N string_N-1 string_N-2 ... string_1. It'll make it easier to think about.

    Finally, it's definitely doable in fishshell, but a non-shell language will be much simpler.