# F# functions implementing polynomial arithmetic [closed]

I have a couple of functions for doing basic arithmetic on polynomials.

let rec mulC x ys =
match (x,ys) with
| (_,[])                    -> []
| (x,y::ys) when x > 0    -> x * y::mulC x ys

let rec addE xs ys =
match (xs, ys) with
| ([],[])                   -> []
| ([], y::ys)            -> y::ys
| (x::xs,[])             -> x::xs
| (x::xs, y::ys)      -> (x + y) :: addE xs ys

let mulX xs =
match xs with
| []                    -> []
| xs                    -> 0::xs

let rec mul qs = function
| []                    -> []
| x::tail               -> addE (mulC x qs)
(mulX(mul qs tail))


## closed as unclear what you're asking by Graipher, Mast, alecxe, Edward, pacmaninbwOct 3 '17 at 15:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Stack Overflow is a better place for this question and you seem to have copied it there already. I suggest you delete this one. – TheQuickBrownFox Oct 1 '17 at 11:48
• As per the help center, we cannot review broken code, not can we explain how/why code behavior. Since this is already cross-posted with Stack Overflow, I have taken the liberty to remove the off-topic portion of the question, and kept the working version for open-ended critique. – 200_success Oct 1 '17 at 15:05

| (x,y::ys) when x > 0    -> x * y::mulC x ys

| (x::xs, y::ys)      -> (x + y) :: addE xs ys


Why the inconsistency in parentheses and whitespace? For what it's worth, I think the second is more readable.

let rec mulC x ys =
match (x,ys) with
| (_,[])                    -> []
| (x,y::ys) when x > 0    -> x * y::mulC x ys


What about the case x = 0, ys is non-empty? Don't you get a warning? (I also find it odd that the test is >. I infer that you're only intending to handle non-negative coefficients, but that seems like an unnecessary restriction).

let rec addE xs ys =
match (xs, ys) with
| ([],[])                   -> []
| ([], y::ys)            -> y::ys
| (x::xs,[])             -> x::xs
| (x::xs, y::ys)      -> (x + y) :: addE xs ys


There's no need to pattern-match a structure if you're not using the structure. This could be simplified to

let rec addE xs ys =
match (xs, ys) with
| ([], ys)            -> ys
| (xs,[])             -> xs
| (x::xs, y::ys)      -> (x + y) :: addE xs ys


Why are the three auxiliary functions in the top-level scope? I admit that I don't know F#, but the languages that it's heavily inspired by would allow them to be defined in a let clause inside the definition of mul, keeping the top-level namespace clean.