Batch Haskell ghci commands together into a file

Asked 2 years ago, Updated 2 years ago, 135 views

Is it possible to combine ghci into a file and process it in bulk?For example,

test.hs

1+2
   3*5

I would like to get the following output from ghci by entering the file

Prelude>1+2
    3
    Prelude > 3*5
    15
    Prelude > 

We tried the following commands and the following in ghci interactive mode:l test.hs, but all of them received Parse error:naked expression at top level and did not get the same results as above.

ghcitest.hs
    runhc test.hs

haskell

2022-09-30 21:13

2 Answers

If you want the command to always run, write the command in the .ghci file in the current directory and run ghci without any arguments to load it automatically.
If you want to separate files, you can create a file such as test.ghci and run it with ghci-ghci-script test.ghci.
These files are called ghciscripts and are not Haskell sources, so naturally loading will result in compilation errors.


2022-09-30 21:13

If only the results are acceptable, there is :s (:script).

Prelude>:test.hs
3
15
Prelude >


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.