【发布时间】:2011-04-23 15:30:22
【问题描述】:
这是我第一个使用 Haskell 的程序。我写它是为了将我读到的关于 FP 的所有内容付诸实践。我要弄清楚的第一件事是如何对要从数据库中提取的数据进行建模(最终我也会写入数据库)。我从我的users 表开始写了这样的内容
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的恒等函数,因此非常重要。 -
你好。我看到您正在使用带有 DB 的 Haskell,以及 Scala 中的代码。你能看看我的问题吗?当您拥有像我们通常在 FP 中那样做的不可变对象时,如何处理数据库?您是否使用可变类进行数据访问? stackoverflow.com/questions/12882099/…
标签: data-structures haskell types