haskell中的可选参数 [英] optional arguments in haskell

查看:118
本文介绍了haskell中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已声明了自己的类型:

I have declared my own type:

data Book = Bookinfo {
              bookId :: Int,
              title :: String
          } deriving(Show)

>

and now:

x = Bookinfo

一切正常,有效语句

但是使bookId x引发错误。

but making bookId x throws an error.

将能够处理Haskell中的错误,这将是确定,但现在我不能这样做所以Im好奇如何使字段的指定值不使用默认值,什么值是什么时,我不给字段的vcalues

If I would be able to handle errors in Haskell that would be ok but right now I cant do this So Im curious how to make not specified values of fields take default value, and what exactly value is there when I'm not giving vcalues of fields in construcotr ?

感谢您的帮助

推荐答案

-- this one is not a Book but actually a function that can make one:
alternativeCtrFunc = Bookinfo

defaultBook = Bookinfo { bookId = 3, title = "Welcome to the Monkey House" }

x = defaultBook
y = defaultBook { bookId = 7 }
z = defaultBook { title = "The Cider House Rules" }

这篇关于haskell中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆