The composition of functions in a list of functions!

后端 未结 5 2023
灰色年华
灰色年华 2020-12-31 20:47

I need to define a function \'Compose\' which takes a list \'L\' which is a list of functions. When I specify a parameter that will suit all the functions in the list, the l

5条回答
  •  执笔经年
    2020-12-31 21:28

    Dan kind of gives it away, but here's a hint on how to do it yourself. You can recurse over numbers:

    0! = 1
    n! = (n-1)! * n
    

    You can also recurse over structure. A list, for example, has a recursive structure, broken down into two cases: an empty list, and an item followed by the rest of the list. In no particular language:

    List :=   Item x List 
            | Nil
    

    Item marks the head of the list, x is the value stored in the head, and List is the tail. In this grammar, your list would be written:

    Item ( fn N -> N + 1 ) Item ( fn N -> 2 * N ) Nil
    

    The rule for a list in the syntax your professor invented could be written recursively as:

    List :=   x ^ List
            | #
    

    A function on a list must recurse over this structure, which means it handles each of the two cases:

    sum l:List = Nil -> 0
               | Item x xs:List = x + sum xs
    

    The recursion, specifically, is the term sum l:List = x + sum xs. Writing this function using the professor's syntax left as an exercise.

    In your problem, your metafunction takes a list of functions and must return a function. Consider each case, the empty list and an item (the head) followed by a list (the tail). In the latter case, you can recursively use your function to get a function from the tail, then combine that somehow with the head to return a function. That's the gist, at any rate.

提交回复
热议问题