# Lagrange (Help please)

**URL:** <https://discuss.ocaml.org/t/lagrange-help-please/9821>\
**Category:** Learning\
**Created:** [May 9, 2022, 7:00pm UTC](https://discuss.ocaml.org/t/lagrange-help-please/9821 "2022-05-09T19:00:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![janelidzemarita](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/janelidzemarita/32/3685_2.png) [@janelidzemarita](https://discuss.ocaml.org/u/janelidzemarita)\
**Post date:** [May 9, 2022, 7:00pm UTC](https://discuss.ocaml.org/t/lagrange-help-please/9821/1 "2022-05-09T19:00:52Z")

</div>

So, I need help trasforming these thee functions to just one using List.fold\_left

let rec poly lst a x\_j= match lst with

|[] → 1.

| (x\_k,y\_k)::ys -\>if x\_k == x\_j then poly ys a x\_j

else ((a -. x\_k) /. (x\_j -. x\_k)) \*. poly ys a x\_j;;

let rec lagrange\_helf lst l a = match lst with

|[] → 0.

| (x\_j,y\_j)::xs → (poly l a x\_j) \*. y\_j +. lagrange\_helf xs l a;;

let lagrange arr a = lagrange\_helf arr arr a;;

This is as fas as I got, but I don’t think it’s rigt

let rec lagrange lst a = match lst with [] → 0.

|(x\_j,y\_j)::xs → List.fold\_left (fun lst l a →

```
                List.fold_left (fun lst a x_j -> a -. x_k) /. (x_j -. x_k )x_k x_j) xs

```

;;

---

<div class="post-metadata">

**Author:** ![jfeser](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jfeser/32/2093_2.png) [@jfeser](https://discuss.ocaml.org/u/jfeser)\
**Post date:** [May 9, 2022, 8:10pm UTC](https://discuss.ocaml.org/t/lagrange-help-please/9821/2 "2022-05-09T20:10:40Z")

</div>

Is this a homework problem?

You might take a look at the other thread: [Lagrange Algorithm Implementation for Ocaml (help pls)](https://discuss.ocaml.org/t/lagrange-algorithm-implementation-for-ocaml-help-pls/9797).
