Shopping cart as a set
In this recipe, we will create a shopping cart for books. The books are uniquely identified by ISBN numbers. If we add the same item in the shopping cart, we should be able to update the existing item or insert a new item in the shopping cart. We will use set as a container for shopping items.
How to do it...
- Create a new project
shopping-cart
using thesimple
Stack template:
stack new shopping-cart simple
- Open
shopping-cart.cabal
and add a dependency on thecontainers
library in thebuild-depends
subsection of theexecutable
subsection:
executable shopping-cart hs-source-dirs: src main-is: Main.lhs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , containers
- Open
src/Main.hs
; we will add our code here. Import the moduleData.Set
:
module Main where import Data.Set as Set
- Create a type to represent a book. The book contains the...