Haskell : loading ALL files in current directory path

萝らか妹 提交于 2019-11-28 08:16:03

问题


The command (in GHCi)

:load abc

Loads the functions in the file abc (which must exist in the current directory path). How would I load all the files in the current directory path? Thanks

----------------------------------------------------------------------------------

[RESPONSE TO POST BELOW]

Hi Rotskoff, thanks I tried your suggestion but I could not get it to work, so I think I must have misunderstood something.

I created 3 files test.hs, test1.hs, and test2.hs as follows :

->

--test.hs
import NecessaryModule

->

--test1.hs
module NecessaryModule where

addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--test2.hs
module NecessaryModule where

addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b

Then when I did :

:load test

I got the error message :

test.hs:1:8:
    Could not find module `NecessaryModule':
      Use -v to see a list of the files searched for.

Thanks

---------------------------------------------------------------------------------

Thanks. This is what I did to get it to work (following Rotskoff's suggestion) :

->

--test.hs
import NecessaryModule1
import NecessaryModule2

->

--NecessaryModule1.hs
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--NecessaryModule2.hs
addNumber2 :: Int -> Int -> Int 
addNumber2 a b = a + b

回答1:


Presumably you mean Haskell source files, because you can't use the :load command in GHCi for anything else.

At the top of the source file that you load, include the line:

import NecessaryModule

For each of the source files, make sure to name the modules, e.g.,

module NecessaryModule where

should appear. GHCi will automatically link all the files.

If you're trying to import data, take a look at System.Directory in the documentation.




回答2:


It's better if the filenames and the names of the modules will be the same:

➤ mv test1.hs NecessaryModule.hs
➤ ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :load test
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.

since the :load command load module(s) (by filenames) and their dependents (as you can read by typing :help or :? in the GHCi prompt).

Also the :load command erase all previous declarations which was defined in the current GHCi session, so for loading all files in the current directory you can do something like this:

Prelude> :q
Leaving GHCi.
➤ ghci *.hs
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.

<no location info>:
    module `main:NecessaryModule' is defined in multiple files: NecessaryModule.hs
                                                            test2.hs
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.
➤ rm test2.hs
➤ ghci *.hs  
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.
*NecessaryModule> 


来源:https://stackoverflow.com/questions/10268692/haskell-loading-all-files-in-current-directory-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!