I define 2 discriminated unions: "Direction" and "TurnCommand":
type Direction =
| South of string
| East of string
| North of string
| West of string
type TurnCommand =
| Left of string
| Right of string
Then I define function to have new Direction after making TurnCommand:
type Turn = Direction -> TurnCommand -> Direction
Here is not working implementation of this function:
let Do:Turn = fun(startDirection) (turn) ->
match startDirection, turn with
| South, Left -> East
| East, Left -> North
| North, Left -> West
| West, Left -> South
| South, Right -> West
| East, Right -> South
| North, Right -> East
| West, Right -> North
There is error: " The constructor applied to 0 arguments but expects 1 ". I understand that it expects string value but I need to match types here. Thanks!
When you defined your constructors (eg South of string
), you said that they required a string
argument. When pattern matching on those constructors, you have to use a variable pattern to store the value given to the constructor (or _
to ignore it) and when constructing a value you have to provide a string too: South s1, Left _ -> East "a string"
. If you don't need any kind of value associated with your constructors just drop the of string
part from their definition.
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.