Monday, April 27, 2020

Another quick guide for Haskell Stack

When you have Main.hs with no stack project, how can you build and run it?


In old days, I used Hugs or GHCi just to load a single Haskell file without any fancy project configurations. Sometimes the Haskell file required me to install some extra libraries such as network. Then I had only to give Hugs or GHCi an option to inform it of a  path where the libraries reside. 

How can you do this with Haskell Stack? This is the topic of this article. I found out it is easy!


$ ls 
Main.hs

$ stack init

$ stack ghci --no-load
Prelude> :l Main
Main.hs:3:1: error:
          Could not find module 'Network.Socket'
...
Prelude>:q

$ stack install network

$ stack ghci --no-load
Prelude> l: Main
Ok, one module loaded.
*Main>


This is it!! Isn't it surprisingly simple? But why does nobody explain it to me? :)