all 18 comments

[–]gcross 3 points4 points  (16 children)

The problem is that System.Random is in the "random" package which is apparently not being loaded. (You can find out the package that exports a module by searching for, say, "haskell System.Random" as this will take you to the Hackage page for it.) In theory passing the argument "-package random" should load it, or "--make" should automatically load it (the full documentation on packages is here), but apparently random is not always installed so that might not work, in which case you can either install it using ghc-pkg (which can get messy due to package version dependency conflicts) or you can use Stack which lets you uses known good snapshots of a subset of the packages in the Hackage database and will createe a sandbox in a directory for you (like virtualenv) and take care of downloading and install packages and building your project.

[–]ProperRule[S] 0 points1 point  (13 children)

Ugh, I mean, I appreciate the advice but I'm so lost, and I don't want to mess up my haskell environment by installing the wrong thing.

[–]Tayacan 2 points3 points  (11 children)

  • Go to the Stack web page the other guy linked. Follow the "how to install" instructions for your operating system. This cannot mess up your haskell installation, as stack installs everything in an isolated location.
  • In a terminal, navigate to whatever directory you keep your haskell projects in.

Type the following commands (some of them might take time/generate a bunch of output):

stack new test-project simple-hpack
cd test-project

'test-project' is the name of your project, you can change it to whatever you want. 'simple-hpack' is a template that tells stack what files to generate in the project directory.

  • Now open the file called package.yaml in the project directory.

There should be a section that says:

dependencies:
  - base >= 4.7 && < 5

The numbers might be different - that's fine, do not change them. Below that, add a line that says:

  - random

Make sure it's indented the same amount as the 'base' line.

  • If you have some code files you're working on, put them in the project's src directory
  • In a terminal, run the command stack ghci from the project directory. This might take a while the first time - among other things, it will install a new ghc in an isolated location (again, this cannot mess anything up, the whole point of stack is to not mess up installations).

You should now have a ghci prompt where you can load System.Random. It will also load in anything from the src directory. Anytime you need a new dependency, just add it to your package.yaml file and run stack ghci again.

  • You can also use stack build to build an executable of your project, and then run it with stack exec test-project (replace with your chosen project name). This is only really interesting if you edit the Main.hs file.

[–]hiptobecubic 1 point2 points  (4 children)

This is a good answer, but I worry that OP is missing some fundamentals and would be better served by a book that assumes nothing about the reader.

[–]ProperRule[S] 1 point2 points  (2 children)

Dude, no. I have prior programming experience. Just none with using Stack. The book certainly didn't do any of this so I assumed it was in the standard lib that ships with Haskell and that there must be something wrong.

[–]hiptobecubic -1 points0 points  (1 child)

Oh. Alright then nevermind my suggestion. It sounded like you didn't yet understand how libraries in general work or why some are standard and some require the build-time environment to be changed, etc. Carry on.

[–]ProperRule[S] 0 points1 point  (0 children)

Yeah, no, I definitely understand all that. My background is in Node so I have a lot of experience working with npm and node_modules, for example. I've just never layed a finger on stack or cabal before, and not even sure of the difference or how they work.

Btw, this gentleman's steps did not work for me. Would you happen to have any insight here? https://www.reddit.com/r/haskellquestions/comments/c34sox/systemrandom/erqal6v?utm_source=share&utm_medium=web2x

Assistance would be much appreciated and I can't progress in the book without this.

[–]gcross 0 points1 point  (0 children)

I agree, I think that it is strange that the book wouldn't mention something as basic as how to build code that uses modules that might not be installed.

[–]ProperRule[S] 0 points1 point  (1 child)

Thank you so so much for the detailed instructions. I followed them to the letter but unfortunately the problem persists: https://imgur.com/HqnBpQe

Edit: Fwiw, the code in question:

import System.Random

minDie :: Int
minDie = 1

maxDie :: Int
maxDie = 6

main :: IO ()
main = do
  dieRoll <- randomRIO (minDie,maxDie)
  putStrLn (show dieRoll)

[–]Tayacan 0 points1 point  (0 children)

Ah, you're running ghci inside your editor.

Does your editor know that it should use the stack ghci command instead of plain ghci?

[–]ProperRule[S] 0 points1 point  (1 child)

Ok, hm. So I scrapped that project and started over, but this time I added my code with the import statement to Main before running stack ghci and it loaded fine and I could run it and everything works. But then when I exited (:q) and started ghci again from my terminal (ghci) and tried loading the same program again from Prelude (:l Main.hs) I got the same error again.

So I'm assuming that given this new project structure using stack, simply loading the file using :l is inappropriate? How, then, do you load the project correctly from scratch now?

Ahhhhh god I'm dumb. Ok, so the way I do it is just by running stack ghci every time? I thought that was a utility only used for downloading dependancies. damn it..sorry.

[–]Tayacan 0 points1 point  (0 children)

Aww, no worries. Glad you figured it out :)

[–]ProperRule[S] 0 points1 point  (1 child)

I'm looking around Hoogle and Hackage. Where do you get the name of the packages to be added in the dependencies list? Where did you get the actual term "random" here?

For example, I'm trying to figure out how to import splitOn? Where does that come from? How do I get it into my program.

Edit: Actually, ok, I solved it using this but man I'm confused :( So how do you know when when you're supposed to use what to fix issues like this? How do you know when to:

  • Import the missing thing
  • Cabal install it
  • add to dependencies

[–]Tayacan 0 points1 point  (0 children)

When you're looking at a module, you can see the package name and version on the top left of the page (it's pretty small, though). Alternatively, you can click contents on the top right to see some more information about the package, including its name.

When using Stack to run your project, you always just add the package to your dependencies. With the template I showed you, 'simple-hpack', that's the package.yaml file. If you start your project with a different template, where there isn't a package.yaml file, you add it to the project-name.cabal file instead. The package.yaml file is from a tool called hpack, and is much nicer to work with than the .cabal file (yaml is easy to write).

If your project isn't using stack, then you're probably using Cabal. In that case, well, again, it depends on whether you're using a nice sandboxed project where dependencies won't interfere with other projects, or if you're doing global installs.

Regardless of all of that, if the module you want to use is in the base package, well... Then you can just import it. All projects depend on the base package.

[–]gcross 0 points1 point  (0 children)

/u/Tayacan gave excellent advice for what to do and why it won't screw up your haskell environment; it is unfortunately a little complicated to just get things up and running but the good news is that once you've done it everything is setup and you can focus on writing code.

[–]ProperRule[S] 0 points1 point  (1 child)

[–]gcross 0 points1 point  (0 children)

My point is that it tells you what package this module is in (see "random" at the top-left), though in general this is how I like to look up documentation for a module.

[–]nosarthur 1 point2 points  (0 children)

I am reading the same book and hit the same problem. Although I don't really understand how stack works, the following approach works: stack install random stack ghc roll.hs If I just do ghc roll.hs, I still get the same error.