Can you advise on how to represent parameterized data structures in haskell? e.g. in an application which represents contents of a garments shop, I might have instances of racks for men's and women's clothes. These parameters can be hierarchical, e.g. based on age groups. So I can have a rack(men(under14)), or a rack(women(adult)). Also, some constraints may apply to the rack parameters, e.g. no baby clothes. And as with java classes, I would need some member functions which would operate on these instances.
相关问题
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
- Haskell split a list into two by a pivot value
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- foldr vs foldr1 usage in Haskell
- List of checkboxes with digestive-functors
- How does this list comprehension over the inits of
- Replacing => in place of -> in function type signa
In Haskell, there is no such thing as "member" functions. All functions are simply functions.
Sure. First of all,
Rack
sounds like it's just a list, so you could make a type synonym.I'll just introduce you to a few features of Haskell that I think might be useful to you. This isn't the only way to do it, but I think it's a decent way to start. The first feature is called Algebraic data types (ADTs):
The type
Gender
has two possibilities,Male
andFemale
. This particular example is basicallyBool
in disguise. Another ADT:I'm no expert on clothing sizes, so tweak as necessary. Notice I've derived
(Eq, Show, Ord)
.Eq
gives us the ability to use==
on values of that data type,Show
lets us useshow
on values of that data type (similar totoString
), andOrd
lets us use comparison operators. With the definition I have provided,Baby < PreTeen
will evaluate toTrue
.Now, then. Let's define clothing as another ADT. (
--
begins a single-line comment)Here I've added fields to the options. You can create an article of clothing using one of the constructors. For example,
Shirt Male Baby
creates a value of typeClothing
.As you continue to learn Haskell, you may want to try using Generalized Algebraic Data Types (GADTs) and Typeclasses. You will probably need to use these features if you wish to create a
Rack
data type with functions on it that leverage the type of the rack to preserve predicates such as "this rack only has adult male clothing". However, I imagine these things are a bit over your head right now; instead try dave's approach with my data types, and run through some good introductory material to Haskell, such as Learn You a Haskell.How about something like this:
You create a
Rack
with a function that specifies whether a particular item of clothing may be put on the rack. This function can use any property ofClothing
(not defined in my example) it likes.e.g. if
isBabyClothing :: Clothing -> Bool
returnsTrue
when given an item of baby clothing, then you create an empty baby clothes rack withemptyRack isBabyClothing
.Use the
allClothesOnRack
function to get a list of all the clothes on the rack.e.g.
allClothesOnRack (emptyRack isBabyClothing)
will always yield[]
.Use the
withClothing
andwithoutClothing
functions to add/remove an item of clothing to/from the rack. They each returnNothing
if this is not possible (either the item of clothing may not be put on that rack, or it is not present on the rack and so cannot be removed), orJust
the updated rack otherwise.