In haskell, how can I refer to the code as a function

Currently, I have an application in which there is a menu that will perform the following functions: add, delete and view. I would like to know how I can refer to the code as a function.

The code I'm trying to use is as follows:

putStrLn "Please enter the username:" addName <- getLine appendFile "UserList.txt" ("\n" ++ addName) 

Should I use the let function? For instance:

 let addUserName = putStrLn "Please enter the username:" addName <- getLine appendFile "UserList.txt" ("\n" ++ addName). 
+4
source share
1 answer

First of all, you use the let keyword when you are in GHCi because you are in the IO monad. Usually you do not need to define a function in the source code. For example, you might have a file called "MyProgram.hs" containing:

 addUserName = do putStrLn "Please enter the username:" addName <- getLine appendFile "UserList.txt" ("\n" ++ addName) 

Then in GHCi you enter:

 ghci> :l MyProgram.hs ghci> addUserName 

(This is: l for: load, not a number). In fact, you can define a function in GHCi, but it's a bit of a pain if it's not single-line. This will work:

 ghci> let greet = putStrLn "Hello!" ghci> greet Hello! 
+8
source

All Articles