-1

sorry to bother you. I'm new to haskell and trying to define a new type Person.

I'm using the GHCI compiler.

I'm running the file new.hs which includes:

Name = String
Age = Int
Weight = Int
Person = (Name, Age, Weight)

but I get not in scope errors. Can anyone help?

Jeremy D helped me with this and I solved that but how can I add a function such as:

isAdult :: Person -> Bool
George = ("George", 20, 80)
isAdult George = if Age >=18 
4

1 回答 1

4

尝试:

type Name = String
type Age = Int
type Weigth = Int
type Person = (Name, Age, Weigth)

简单介绍,看这里

为了回答您的第二个问题,这是我所做的:

newtype Name = Name String deriving (Show)
newtype Age = Age Int deriving (Show)
newtype Weigth = Weight Int deriving (Show)
newtype Person = Person (Name, Age, Weigth) deriving (Show)   

isAdult :: Person -> Bool
isAdult (Person(_, Age a, _)) =  a > 18

执行时:

*Main> let p = Person(Name "Jeremy", Age 18, Weight 199)
*Main> p
Person (Name "Jeremy", Age 18, Weight 199)
*Main> isAdult p
False
*Main> let p = Person(Name "Jeremy", Age 20, Weight 199)
*Main> isAdult p
True
*Main> 
于 2013-10-03T10:30:48.307 回答