Вопрос

I load this file as a module to Hugs, but get this error:

ERROR file:.\Hugs.hs:38 - Syntax error in input (unexpected keyword "let")

data MetricUnit = Meter
            | Liter
            | KiloGram
              deriving (Show, Eq)

data ImperialUnit = Yard
              | Gallon
              | Pound
                deriving (Show, Eq)

data Measurement = MetricMeasurement Double MetricUnit
             | ImperialMeasurement Double ImperialUnit
               deriving (Show)

symbol :: MetricUnit -> String
symbol x
  | x == Meter = "m"
  | x == Liter = "L"
  | x == KiloGram = "kg"

convert (MetricMeasurement x u)
  | u==Meter    = ImperialMeasurement (1.0936*x) Yard
  | u==Liter    = ImperialMeasurement (0.2642*x) Gallon
  | u==KiloGram = ImperialMeasurement (2.2046*x) Pound

convert (ImperialMeasurement x u)
  | u==Yard   = MetricMeasurement (0.9144*x) Meter
  | u==Gallon = MetricMeasurement (3.7854*x) Liter
  | u==Pound  = MetricMeasurement (0.4536*x) KiloGram

let fac n = if n == 0 then 1 else n * fac (n-1)   --This line doesn't work
let m = MetricMeasurement 2 Meter
Это было полезно?

Решение 2

Remove let:

fac n = if n == 0 then 1 else n * fac (n-1)

m = MetricMeasurement 2 Meter

Другие советы

You can't have let statements at the top level. Simply write

fac n = if n == 0 then 1 else n * fac (n-1)
m = MetricMeasurement 2 Meter

As the others have pointed out, you don't use let for a top level definition, just for local definitions within a function.

I've answered because I couldn't help but point out that you could improve a lot of your code by using pattern matching.

For symbol, the matching can go at the top level:

symbol :: MetricUnit -> String
symbol Meter = "m"
symbol Liter = "L"
symbol KiloGram = "kg"

Whereas for the other two, you can use a case statement to match on the u, like this:

convert (MetricMeasurement x u) = case u of
   Meter    -> ImperialMeasurement (1.0936*x) Yard
   Liter    -> ImperialMeasurement (0.2642*x) Gallon
   KiloGram -> ImperialMeasurement (2.2046*x) Pound

and convert (ImperialMeasurement x u) would be similar.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top