How to write a ‘Function’ in Haskell 98
After days of trying just figured out how to write a function in haskell 98 (see how noob
)
‘Functions’ in functional programming plays a major role..
There are few thing we should concern before writing a function ..
- Code Indentation and case sensitiveness both of these aspects are really needed [We know that case sensitiveness is required in languages like C++ ] BUT first time in functional programming Code indention is also highly required.
- You CANNOT write functions in the Hugs/GHCI native console [This like this the Hugs console is just a place to test nooby stuff not to write real code
]….
Where should you write code ?
Use a text editor
[Notepad++ etc.] and save that file with extension .hs
and required to attach that file to the Hugs interpreter …. like this
In Hugs:
File Menu –> Module Manager
Click add and attach ur .hs file to the interpreter… after attaching if u see any messages in red color in the hugs console .. there is some thing wrong with ur code !
Undefined data constructor "x"
Syntax error in declaration (unexpected `}’, possibly due to bad
If u get this kind of error message just check for code indentation and case
Coding
This is a function to find factorial using Haskell
fact :: Int –> Int
fact n = if n == 0 then
1
else
n * fact (n-1)
Green stuff are the function definition n
C++/C such as :
return_type function_name (Parameters)
In Haskell:
Function_Name :: Return –> Input Parameters
{ Body }
(Make sure to keep the indentation if not function will throw errors !)