I am trying to do something like the following where I want to return an error message in case the state of a State-Monad isn't fulfilling a certain condition, can it be done without using lift and ExceptT or other transformer monad?
type Log = State [String] ()
stateCheckDuplicate :: String -> Either String Log
stateCheckDuplicate elem =
Right (do
log <- get
if lookup elem log /= Nothing then Left "Duplicate elements"
else
(put (elem:log)))
If I understand what the
stateCheckDuplicateshould do, you need the type ofstateCheckDuplicateisomorphic toString -> [String] -> Either String [String]. Then your implementation will look like this:As you can see, there is nor State, nor ExceptT, nor liftXXX. But maybe there is one problem, this function is "hard" to compose with others functions. To solve this problem need understand what you try to do.
Update on:
For using this operation in do-block, the result type should be an instanca of class
Monad. You can make your own type for this purpose.But compare how easy it will be with transformers:
If you don't want type alias you can do like this: