1
votes

I need to write a pipe function in OCaml such that pipe [f1;...;fn] (where f1,...,fn are functions!) returns a function f such that for any x, f x computes fn(...(f2(f1 x))).

I need to write it using List.fold_left and need to fill in the parameter of the function

let pipe fs =
   let f a x = "fill in this part" in
   let base = fun x ->x in
   List.fold_left f base fs;;

I already filled in the base. If the first parameter to pipe is an empty list, it returns the second parameter. Ex: pipe [] 3 = 3. i know for the let f a x part I want to perform function x of the accumulated functions a. I'm just not sure how to write that. I wrote let f a x = x a but that gave me an error when i tested pipe [(fun x -> x+x); (fun x -> x + 3)] 3

it should run x+x on 3 then run x+3 on the result and give me 9 but it gave it a type error when i tried to use let f a x = x a for the fill in part

# let _ = pipe [(fun x -> x+x); (fun x -> x + 3)] 3;;
File "", line 1, characters 24-25:

Error: This expression has type 'a -> 'a
       but an expression was expected of type int 

What is the correct format to create a function that takes in 2 functions and runs them on each other. Ex: make a function that takes in functions a and b and runs b on the result of a.

3
If I understand what you're asking, the answer to your last question looks like this: let f a b x = b (a x).Jeffrey Scofield
yes except im not allowed to change the left side so i cant have the b in there left side needs to be let f a x where a is the accumulator of functions and x is the next function that is popped off the list. so for example with let _ = pipe [(fun x -> x+x); (fun x -> x + 3)] 3 the base function would be fun x -> x which is the first one stored in a. It then needs to pop off the fun x-> x+x and store that function running on the base one in the accumulator. Then it pops the last one, does the same thing and its done. im assuming thats how its meant to work.Anatoliy Sokolov
so for that example it would be like call fun x -> x+ 3 on the result of fun x->x+x which is called on the result of fun x ->x which is called on 3. so it should give you 9Anatoliy Sokolov
not exactly sure what you mean by thisAnatoliy Sokolov
I worked through your exercise, so I can give a hint. Your function f has to return a function. So it should return something that looks like this: fun z -> .... You need to figure out what the returned function should be doing with its argument z.Jeffrey Scofield

3 Answers

0
votes

Well, base is a function like this fun x -> ....

Similarly, your function f needs to return a function, so assume it returns something that looks like this:

fun z -> ...

You have to figure out what this function should be doing with its argument z.

1
votes

To evaluate (fold_left g init fs) x:

  • when fs is empty, (fold_left g init fs) x = init x. In your case, you want it to be x.
  • when fs = fs' @ [fn]: according to what you would like to be true, the expression should evaluate to fn (fold_left g init fs' x) but using the definition of fold_left it evaluates also to (g (fold_left g init fs') fn) x.

Hence if the following equations are true:

  • init x = x

  • (g k f) x = f (k x)

the problem is solved. Hence, let us define init = fun x -> x and g k f = fun x -> f (k x).

0
votes

figured it out just needed that z in there for a and x to call