这是我第一个使用Haskell的程序。我正在写这篇文章来实践我读过的有关FP的所有内容。我试图弄清楚的第一件事是如何建模我将从数据库中提取的数据(最终我也会写入数据库)。我开始与我users
表,写了这样的事情DB支持应用程序的Haskell数据结构
module Model (User) where
class Audited a where
creationDate :: a -> Integer
lastUpdatedDate :: a -> Integer
creationUser :: a -> User
lastUpdatedUser :: a -> User
class Identified a where
id :: a -> Integer
data User = User {userId :: Integer}
instance Identified User where
id u = userId u
和
module Main (main) where
import Model (User)
data Point = Pt {pointx, pointy :: Float}
instance Show Point where
show (Pt x y) = "(" ++ show x ++ ", " ++ show y ++ ")"
main :: IO()
main = do
print $ Pt 1 2
(该Point
东西只是我的测试...这是我的第一个Haskell代码有史以来)
这段代码没有编译,但我并没有真正关心这个问题 - 最重要的是让我的类型设置得很好。
这里是我有
- 什么是在Haskell基于记录的数据进行建模的最好方法问题的清单?
- 我的大部分表格都有审计信息和不透明的ID。我如何利用Haskell类型系统来利用这一点?你可以看到我创建了Audited和Identified类。这是一个好方法吗?
- 这是Haskell的一个很好的应用程序吗?我正在考虑使用Clojure,因为它可以与Java互操作(此应用程序目前用Java编写)。
顺便说一句,定义一个函数'id'不是一个好主意。通常,'id'是定义为'id x = x'的标识函数,因此非常重要。 – fuz 2011-04-23 19:04:58
你好。我看到你正在使用Haskell和一个数据库,并且还使用了Scala中的代码。你能看看我的问题吗?如果像我们通常在FP中那样拥有不可变对象时如何处理数据库?你使用可变类来访问数据吗? http://stackoverflow.com/questions/12882099/how-to-update-object-in-mongo-with-an-immutable-salat-case-class – 2012-10-20 22:21:44