简体   繁体   中英

How to properly create and use polynomial type and term type in f#

I'm trying to do this exercise: 在此处输入图片说明

I'm not sure how to use Type in F#, in F# interactive, I wrote type term = Term of float *int , Then I tried to create a value of type term by let x: term = (3.5,8);; But it gives an error. Then I tried let x: term = Term (3.5,8);; and it worked. So Why is that?

For the first function, I tried:

let multiplyPolyByTerm (x:term, p:poly)=
    match p with
    |[]->[]

But that gives an error on the line |[]->[] saying that the expression is expecting a type poly, but poly is a in fact a list right? So why is it wrong here? I fixed it by |Poly[]->Poly[] . Then I tried to finish the function by giving the recursive definition of multiplying each term of the polynomial by the given term: |Poly a::af-> This gives an error so I'm stuck on trying to break down the Poly list.

If anyone has suggestion on good readings about Type in F#, please share it.

I got all the methods now, However,I find myself unable to throw an exception when the polynomial is an empty list as the base case of my recursive function is an empty list. Also, I don't know how to group common term together, Please help, Here are my codes:

type poly=Poly of (float*int) list
type term = Term of float *int
exception EmptyList
(*
let rec mergeCommonTerm(p:poly)=
    let rec iterator ((a: float,b: int ),  k: (float*int) list)=
        match k with
        |[]->(a,b)
        |ki::kf-> if b= snd ki then (a+ fst ki,b)
    match p with
    |Poly [] -> Poly []
    |Poly (a::af)-> match af with
                    |[]-> Poly [a]
                    |b::bf -> if snd a =snd b then Poly (fst a +fst b,snd a)::bf 
                              else   
*)


let rec multiplyPolyByTerm (x:term, p:poly)=
    match x with
    | Term (coe,deg) -> match p with
                        |Poly[] -> Poly []
                        |Poly (a::af) -> match multiplyPolyByTerm (x,Poly af) with
                                         |Poly recusivep-> Poly ((fst a *coe,snd a + deg)::recusivep)


let rec addTermToPoly (x:term, p:poly)=
    match x with
    |Term (coe, deg)-> match p with
                       |Poly[] ->  Poly [(coe,deg)]
                       |Poly (a::af)-> if snd a=deg then Poly ((fst a+coe,deg)::af)
                                       else match addTermToPoly (x,Poly af) with
                                         |Poly recusivep-> Poly (a::recusivep)




let rec addPolys (x:poly, y: poly)=
    match x with
    |Poly []->y
    |Poly (xh::xt)-> addPolys(Poly xt,addTermToPoly(Term xh, y))


let rec multPolys (x:poly,y:poly)=
    match x with
    |Poly []-> Poly[]
    |Poly (xh::xt)->addPolys (multiplyPolyByTerm(Term xh,y),multPolys(Poly xt,y))

let evalTerm  (values:float) (termmm : term) :float=
    match termmm with
    |Term (coe,deg)->coe*(values**float(deg))

let rec evalPoly (polyn :  poly, v: float) :float=
    match polyn with
    |Poly []->0.0
    |Poly (ph::pt)-> (evalTerm v (Term ph)) + evalPoly (Poly pt,v)

let rec diffPoly (p:poly) :poly=
    match p with
    |Poly []->Poly []
    |Poly (ah::at)-> match diffPoly (Poly at) with
                    |Poly [] -> if snd ah = 0 then Poly []
                                else Poly [(float(snd ah)*fst ah,snd ah - 1)]
                    |Poly (bh::bt)->Poly ((float(snd ah)*fst ah,snd ah - 1)::bh::bt)

As I mentioned in a comment, reading https://fsharpforfunandprofit.com/posts/discriminated-unions/ will be very helpful for you. But let me give you some quick help to get you unstuck and starting to solve your immediate problems. You're on the right track, you're just struggling a little with the syntax (and operator precedence, which is part of the syntax).

First, load the MSDN operator precedence documentation in another tab while you read the rest of this answer. You'll want to look at it later on, but first I'll explain a subtlety of how F# treats discriminated unions that you probably haven't understood yet.

When you define a discriminated union type like poly , the name Poly acts like a constructor for the type. In F#, constructors are functions. So when you write Poly (something) , the F# parser interprets this as "take the value (something) and pass it to the function named Poly ". Here, the function Poly isn't one you had to define explicitly; it was implicitly defined as part of your type definition. To really make this clear, consider this example:

type Example =
    | Number of int
    | Text of string

5           // This has type int
Number 5    // This has type Example
Number      // This has type (int -> Example), i.e. a function
"foo"       // This has type string
Text "foo"  // This has type Example
Text        // This has type (string -> Example), i.e. a function

Now look at the operator precedence list that you loaded in another tab. Lowest precedence is at the top of the table, and highest precedence is at the bottom; in other words, the lower something is on the table, the more "tightly" it binds. As you can see, function application ( fx , calling f with parameter x ) binds very tightly, more tightly than the :: operator. So when you write fa::b , that is not read as f (a::b) , but rather as (fa)::b . In other words, fa::b reads as "Item b is a list of some type which we'll call T , and the function call fa produces an item of type T that should go in front of list b ". If you instead meant "take the list formed by putting item a at the head of list b , and then call f with the resulting list", then that needs parentheses: you have to write f (a::b) to get that meaning.

So when you write Poly a::af , that's interpreted as (Poly a)::af , which means "Here is a list. The first item is a Poly a , which means that a is a (float * int) list . The rest of the list will be called af ". And since the value your passing into it is not a list, but rather a poly type, that is a type mismatch. (Note that items of type poly contain lists, but they are not themselves lists). What you needed to write was Poly (a::af) , which would have meant "Here is an item of type poly that contains a list. That list should be split into the head, a , and the rest, af ."

I hope that helped rather than muddle the waters further. If you didn't understand any part of this, let me know and I'll try to make it clearer.

PS Another point of syntax you might want to know: F# gives you many ways to signal an error condition (like an empty list in this assignment), but your professor has asked you to use exception EmptyList when invalid input is given. That means he expects your code to "throw" or "raise" an exception when you encounter an error. In C# the term is "throw", but in F# the term is "raise", and the syntax looks like this:

if someErrorCondition then
    raise EmptyList

// Or ...

match listThatShouldNotBeEmpty with
| [] -> raise EmptyList
| head::rest -> // Do something with head, etc.

That should take care of the next question you would have needed to ask. :-)

Update 2 : You've edited your question to clarify another issue you're having, where your recursive function boils down to an empty list as the base case — yet your professor asked you to consider an empty list as an invalid input. There are two ways to solve this. I'll discuss the more complicated one first, then I'll discuss the easier one.

The more complicated way to solve this is to have two separate functions, an "outer" one and an "inner" one, for each of the functions you have been asked to define. In each case, the "outer" one checks whether the input is an empty list and throws an exception if that's the case. If the input is not an empty list, then it passes the input to the "inner" function, which does the recursive algorithm (and does NOT consider an empty list to be an error). So the "outer" function is basically only doing error-checking, and the "inner" function is doing all the work. This is a VERY common approach in professional programming, where all your error-checking is done at the "edges" of your code, while the "inner" code never has to deal with errors. It's therefore a good approach to know about — but in your particular case, I think it's more complicated than you need.

The easier solution is to rewrite your functions to consider a single-item list as the base case, so that your recursive functions never go all the way to an empty list. Then you can always consider an empty list to be an error. Since this is homework I won't give you an example based on your actual code, but rather an example based on a simple "take the sum of a list of integers" exercise where an empty list would be considered an error:

let rec sumNonEmptyList (input : int list) : int =
    match input with
    | [] -> raise EmptyList
    | [x] -> x
    | x::rest -> x + sumNonEmptyList rest

The syntax [x] in a match expression means "This matches a list with exactly one item in it, and assigns the name x to the value of that item". In your case, you'd probably be matching against Poly [] to raise an exception, Poly [a] as the base case, and Poly (a::af) as the "more than one item" case. (That's as much of a clue as I think I should give you; you'll learn better if you work out the rest yourself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM