我正在使用GHCI编译器。
我正在运行new.hs文件,其中包括:
Name = String
Age = Int
Weight = Int
Person = (Name, Age, Weight)
但我没有考虑范围错误。有人可以帮忙吗?
Jeremy D帮我解决了这个问题,但是我解决了这个问题,但是如何添加一个函数,如:
isAdult :: Person -> Bool
George = ("George", 20, 80)
isAdult George = if Age >=18
答案 0 :(得分:4)
尝试:
type Name = String
type Age = Int
type Weigth = Int
type Person = (Name, Age, Weigth)
如需简单介绍,请查看here
要回答你的第二个问题,我就是这样做的:
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>