[英]Basic haskell : Defining types
sorry to bother you. 抱歉打扰你。 I'm new to haskell and trying to define a new type Person.
我是Haskell的新手,正在尝试定义一个新的Person类型。
I'm using the GHCI compiler. 我正在使用GHCI编译器。
I'm running the file new.hs which includes: 我正在运行文件new.hs,其中包括:
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: 杰里米·D(Jeremy D)帮助了我,我解决了这个问题,但是我如何添加一个函数,例如:
isAdult :: Person -> Bool
George = ("George", 20, 80)
isAdult George = if Age >=18
try with: 尝试:
type Name = String
type Age = Int
type Weigth = Int
type Person = (Name, Age, Weigth)
For a simple introduction, look here 对于简单的介绍,请看这里
To answer your second question, here is what I did: 为了回答您的第二个问题,这是我的工作:
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
When executing it: 执行时:
*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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.