简体   繁体   中英

How to program haskell with ghci?

I've been reading some material and here I have a question: I saw a fragment of code is like this:

>getNthElem 1 xs = head xs
>getNthElem n [] = error "'n' is greater than the length of the list"
>getNthElem n xs
>    | n < 1     = error "'n' is non-positive"
>    | otherwise = getNthElem (n-1) (tail xs)

Should I type all these lines exactly the same into ghci or should I create a .hs file and put them in, then load it in the ghci?

There are 2 ways to do this:

  1. Use multiline mode within ghci by setting the flag as:

      Prelude> :set +m Prelude> let getNthElem 1 xs = head xs Prelude| getNthElem n [] = error "error1" Prelude| getNthElem n xs Prelude| | n < 1 = error "error2" Prelude| | otherwise = getNthElem (n-1) (tail xs) Prelude| Prelude> 
  2. Create a file and load it as a module to access the types and functions defined in it as

     Prelude> :l myModule.hs 

    And file contents:

     getNthElem :: Int -> [a] -> a getNthElem 1 xs = head xs getNthElem n [] = error "'n' is greater than the length of the list" getNthElem n xs | n < 1 = error "'n' is non-positive" | otherwise = getNthElem (n-1) (tail xs) 

I would recommend using the second option since it's quite easy to mess up indentation in multiline mode within GHCI. Also, make it a habit of adding type signatures before you start defining the function body.

You could write in 1 line:

> let getNthElem 1 xs = head xs; getNthElem n [] = error "'n' is greater than the length of the list"; getNthElem n xs | n < 1 = error "'n' is non-positive" | otherwise = getNthElem (n-1) (tail xs)

Don't forget to write semicolon instead of newline and add let word in the beginning.

You could also use multi-line regime:

> :{
| let getNthElem 1 xs = head xs
|     getNthElem n [] = error "'n' is greater than the length of the list"
|     getNthElem n xs
|       | n < 1     = error "'n' is non-positive"
|       | otherwise = getNthElem (n-1) (tail xs)
| :}
>

The easiest thing is to create a file called eg example.hs and then start ghci at the command line and load the file

$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Prelude> :load example.hs
[1 of 1] Compiling Main      ( example.hs, interpreted )
Ok, module loaded: Main.
*Main> 

Alternatively you can load the file when you start ghci

$ ghci example.hs
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main      ( example.hs, interpreted )
Ok, module loaded: Main.
*Main> 

Note that the > at the start of each line indicates that your file is a literate Haskell file ie it should have the extension *.lhs instead of *.hs. You should either rename your file to *.lhs or remove the > at the beginning of each line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM