Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a function in ghci across multiple lines?

People also ask

How do you write multiple lines in a function Python?

You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.

How do you define a main in Haskell?

The type of main in Haskell is almost always IO () which tells us that main does input/output and its only “return value” is output to a screen or some other kind of effect (in this program, the effect is printing the result of the function evaluation to the screen).


GHCi now has a multiline-input mode, enabled with :set +m. For example,

Prelude> :set +m
Prelude> let fac 0 = 1
Prelude|     fac n = n * fac (n-1)
Prelude|
Prelude> fac 10
3628800

For guards (like your example), you can just put them all on one line and it works (guards do not care about spacing)

let abs n | n >= 0 = n | otherwise = -n

If you wanted to write your function with multiple definitions that pattern match on the arguments, like this:

fact 0 = 1
fact n = n * fact (n-1)

Then you would use braces with semicolons separating the definitions

let { fact 0 = 1 ; fact n = n * fact (n-1) }

Dan is correct, but :{ and :} must each appear on their own line:

> :{ 
> let foo a b = a +
>           b
> :}
> :t foo
foo :: (Num a) => a -> a -> a

This also interacts with the layout rule, so when using do-notation it might be easier to use braces and semi-colons explicitly. For example, this definition fails:

> :{
| let prRev = do
|   inp <- getLine
|   putStrLn $ reverse inp
| :}
<interactive>:1:18:
    The last statement in a 'do' construct must be an expression

But it works when braces and semi-colons are added:

> :{
| let prRev = do {
|   inp <- getLine;
|   putStrLn $ reverse inp;
| }
| :}
> :t prRev
prRev :: IO ()

This will only really matter when pasting definitions from a file, where indentation might change.


It looks like :{ and :} are a pretty new feature. You may need to upgrade GHC.

Edit: confirmed, see http://www.haskell.org/ghc/docs/6.8.2/html/users_guide/release-6-8-2.html


If you don't want to upgrade GHC just for :{ and :}, you'll need to write it all on one line:

> let abs' n | n >= 0 = n | otherwise = -n

I'm not aware of any single definition in Haskell that must be written on multiple lines. The above does indeed work in GHCi:

> :t abs'
abs' :: (Num a, Ord a) => a -> a

For other expressions, such as do blocks, you'll need to use the non-layout syntax with curly braces and semicolons (eugh).


It looks like pasting both lines at once or using control-enter for each new line keeps it all together, at least at https://repl.it/languages/haskell. You'll see 2 dots in the beginning of the second line. Or put it in a file and :load the file (:l main). How come abs doesn't work with negative numbers? Oh you have to put parentheses around the number.

   let abs n | n >= 0 = n 
..           | otherwise = -n
   abs (-1)