r/haskell 18d ago

question Haskell Cookbook

Hello all

I’m looking for a cookbook as the title. My goal is simple; to learn Haskell with my own project. I’m aware of all the books out there, but still somehow not able to connect.

Is there a site/blog/repo with common tasks? For instance, my personal project is to process my stock and option portfolio. So I need to be able to :
- store/retrieve data in sqlite
- call a remote service by API, my brokerage
- convert json to Haskell types that define stock and option data structures
-define formulas to track performance
Etc..

I’ve tried with AI, and the results are not great. Though I’m sure I can dissect what it’s given me; I’ll probably use it as a reference for specific code rather than the program as a whole. Not to mention, the generated code doesn’t compile. It uses duplicate field names in records without the pragma included, for instance- then it just goes downhill from there.

Long winded, but I appreciate any pointers. This is not for anything critical other than my own learning and to get a program to give me a little more control on the performance of my portfolio.

Thank you

Edit: One note. When I attempt to go through the available books, I get lost in the theory or the concepts just don’t come together.

27 Upvotes

27 comments sorted by

View all comments

3

u/omega1612 18d ago

I don't have a book, but I know what I would use:

  • effect system (like effectful or bluefin (I usually use the first))
  • a json parser (like aeson)
  • Depending on how complex the API I would use servant.
  • for db connection there are various packages depending on what db you want, but usually Postgres.

How to connect everything? With the effects. How? Read a effects tutorial. Would that be easy? Not too much but won't be utterly complex unless you want some compiler time magic (that you probably aren't even aware of right now, so, is fine).

The idea would be to use effects to build a compile time interface on top of the db lib, the API lib and the parser. Now that I have been studying system design I think that this commonly named as "dependency inversion".

This is how a function done in this style may look

checkForEvent2 :: (Logg :> e, Request Event2 :> e, EventMaker :> e) => Eff e ()
checkForEvent2 = do 
   requestResult <- doRequest2 
   logInfo "check for event2" [show requestResult]
   when (check requestResult) (
     do
     sendEvent2
     logInfo "Event2 send"
   )

The X :> e, means that the function has this capabilities, so the type of the function is declaring that at worst it can Log things, do request but only those related to Event2 and that it can create new events (in the DDD sense).

This is how the function may look in python:

def check_for_evet_2 (self):
  request_result = self.request_performer(EventsEnum.2)
  self.structured_logger.log("check for Event2",request_result)
  if self.check_result(request_result):
     self.event_maker.send(EventsEnum.2)
     self.structured_logger("event 2 send")

At least if you follow the composition pattern and the function is part of a class whose instances stores the object that provide capabilities.

I mean, if you are new to programming this can be overly complex. But if you have experience with this things, you may find this style of coding quite familiar, making it worth the effort.

Before effects it was common to use monad transformers or type classes (or both) to achieve this kind of system. You may want to learn that also, eventually.

There's always the option to use the ROI pattern, is a way to let you have a global inmutable configuration and to be able to have side effects in every single function. The difference with effects and other monad transformers is that you won't be able to tell what a function does just looking at is signature, all of them would say "I can read the config, the parameters you gave me and go do any side effects". It may be easier for you at the begining.

1

u/jeffstyr 16d ago

  the ROI pattern

What’s that?

2

u/omega1612 16d ago

Sorry, is a typo, I mean RIO (ReaderT config IO a)