Home > Back-end >  How do I add a library to a Stack project? (Haskell)
How do I add a library to a Stack project? (Haskell)

Time:03-11

I know this question has probably been asked a thousand times, but I'm currently learning Haskell, and I'm making a little random number generator / guessing game. I got all the terminal input and output working, and started trying to figure out random number generation, and I can't figure it out. First, I tried adding the random library to the .cabal file, and whenever I ran stack build, it removed it from the file. Then, I tried putting it into the stack.yaml file. After deleting the lock file, and putting it in there, it compiled without an error. So, I tried importing it with import System.Random like I've seen everywhere else, and it didn't work. I tried asking a question on the Github repo, but nobody answered my question.

CodePudding user response:

Do you have a package.yaml file in your directory? Stack will (re-)generate your .cabal file from it every time it runs. So:

  • Add the dependency in stack.yaml only if the library is not on Stackage or you want a different version.
  • Add the dependency in package.yaml.
  • Related