简体   繁体   中英

User defined type Ocaml

I have the following user defined type:

poly = Poly of (float*int) list

and I'm trying to apply pattern matching, however I'm struggling with the case where my Poly is not an empty list, my last try was

Poly [(x,y)::xs]

but I know that it is completely wrong I just struggle formulating it.

There are two ways to write a list (and a list pattern). If you use the :: operator you can have an arbitrary list at the tail of your pattern. If you use the [ ... ] notation, your pattern will only match a list of a specific length.

You are mixing these two pattern types, which is not what you want. (You can of course mix them when matching against an appropriate type.)

Your pattern should (most likely) look like this: Poly ((x, y) :: xs) . Note the parentheses around the body of the pattern. The :: operator has lower precedence than the application of Poly . So you need the parentheses.

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