Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#2150 closed feature request (worksforme)

allow definition of functions in the interpreter

Reported by: ahrivera Owned by:
Priority: normal Milestone:
Component: GHCi Version: 6.8.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by simonpj)

Right now, the only way to define functions is through the .hs files. Other interpreted languages such as Python allow the user to define functions in the command line. This is very helpful when one wants to prototype some quick functions.

One way to do it could be to detect when one declares the function and then enable a mode where one can add definitions. For example:

Prelude> x:: Int ->Int
*definition*> x a = a + 1
Prelude> x 10

Please consider adding this functionality to GHCI.


Change History (4)

comment:1 Changed 10 years ago by simonpj

Description: modified (diff)
difficulty: Unknown
Resolution: worksforme
Status: newclosed

Happily it's there already:

Prelude> let x a = a+1
Prelude> x 10

Multi-line definitions are, admittedly, less convenient. You have to write them on one line, but you can use braces and semicolons to put multiple lines on one line.

Prelude> let { x::Int -> Int; x a = a+1 }

Perhaps there should be a "mode" as you imply above. Something like this:

Prelude> let { 
*def*> x a = a+1
*def*> }

But the details of such a design are not obvious to me.

I'll close this since the feature is there. If it's specifically the multi-line thing you want, then open a new ticket for that.


comment:2 Changed 10 years ago by simonmar

in fact we do have multi-line commands, thanks to Claus:

Prelude> :{
Prelude| let x =
Prelude| 1 + 2
Prelude| :}
Prelude> x

This was introduced in 6.8.2, and is documented:

comment:3 Changed 9 years ago by simonmar

Architecture: UnknownUnknown/Multiple

comment:4 Changed 9 years ago by simonmar

Operating System: UnknownUnknown/Multiple
Note: See TracTickets for help on using tickets.