Haskell does not know the cause of parse error on input|

Asked 2 years ago, Updated 2 years ago, 126 views

There is a heading error in line 6 of the code and I don't know why.
I can't find the cause of the error even though it's indented and there's no extra = in it.
Thank you for your guidance.

data T=T {x::Either_Int, y::Maybe Int}
f=\t->case of
  xy
    Justy
      | x::Right Int->x+y
      | x::Left_->x
    Nothing - > 0
    _->undefined

haskell

2022-09-30 21:44

1 Answers

Do you mean that you want to separate the T type value y from Just and Nothing?
In that case, you must write another case expression as shown below.

data T=T {x::Either_Int, y::Maybe Int}
f=\t->case of
  Text my->
      case my of
        Justy->
          case of
              Right x - > x + y
              Left ->e
        Nothing - > 0
  _->undefined

Unfortunately, as shown in the example above, the distinction between x being Right and Left is also incorrect (mistaken for guard syntax?)

.

To further point out how to use the case expression, values with only one value constructor, such as the T type, do not need to be separated by the case expression.
A pattern match with the arguments of the function should be sufficient, as shown below.

data T=T {x::Either_Int, y::Maybe Int}
f(Tex my)=
    case my of
      Justy->
        case of
            Right x - > x + y
            Left ->e
      Nothing - > 0

Unlike the case expression, remember to enclose it in parentheses, such as (Texmy) when matching patterns directly with arguments in a function.

If you take advantage of both nested pattern matches and pattern matches in function arguments, you can also write as follows:

data T=T {x::Either_Int, y::Maybe Int}
f(T(Right x)(Justy)) = x + y
f(T_Nothing) = 0
f(T(Left)_)=e

一 Please note that all arguments are matched in one pattern match, so the order of matching is slightly changed.

In addition, to eliminate compilation errors for this code, you must further change the definition of the T type.

data T=T {x::Either_Int, y::Maybe Int}

The Either_Int section of the , but the syntax _ cannot be used in type annotations in type definitions.
Even if you correct the case expression, you will get the following error:

Wildcard'_'not allowed
      in the definition of data constructor 'T'
  |
1 | data T=T {x::Either_Int, y::Maybe Int}
  |            

Therefore, you must specify the appropriate type.
In this answer, we will omit which model should be used.I don't know what the questioner wants to do in the first place.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.