Opened 6 years ago

Closed 6 years ago

Last modified 6 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: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

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
*definition*>
Prelude> x 10
11

Please consider adding this functionality to GHCI.

Thanks

Change History (4)

comment:1 Changed 6 years ago by simonpj

  • Description modified (diff)
  • Difficulty set to Unknown
  • Resolution set to worksforme
  • Status changed from new to closed

Happily it's there already:

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

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*> }
Prelude>

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.

Simon

comment:2 Changed 6 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
3
Prelude> 

This was introduced in 6.8.2, and is documented: http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html#ghci-stmts

comment:3 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:4 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple
Note: See TracTickets for help on using tickets.