
data Foo = Foo { a :: Int, b :: String }
instance Show Foo where show f = show (a f) ++ " " ++ show (b f)
foo = Foo { a = 99, b = "green bottles\n" }
main = do print foo let bar = foo { a = 98 } print bar
The above code is from Haskell FAQ. I could not find the "data Foo = Foo }" notation in my Haskell, The Craft of Functional Programming book. What does it mean? Also
instance Show Foo where show f = show (a f) ++ " " ++ show (b f)
I do not understand show (a f) either. And same goes for "foo { a = 98 }" notation. Could you help me on this notation? Thanks

"Cagdas Ozgenc"
Could you help me on this notation?
Perhaps?
data Foo = Foo { a :: Int, b :: String }
This declares a Foo constructor with two named fields, and Int "a" and a String "b". This is equivalent to declaring data Foo = Foo Int String but with a couple of extra features added, among others that you automatically get defined functions "a" and "b" which take a Foo object and return the values of the respective parts.
instance Show Foo where show f = show (a f) ++ " " ++ show (b f)
...so the (a f) and (b f) parts are just extracting the relevant information from the Foo. If you know Lisp, it's fairly similar to defstruct. -kzm -- If I haven't seen further, it is by standing in the footprints of giants
participants (2)
-
Cagdas Ozgenc
-
Ketil Malde