Skip to content

How to write a ‘Function’ in Haskell 98

April 19, 2011

After days of trying just figured out how to write a function in haskell 98 (see how noob Winking smile )

‘Functions’ in functional programming plays a major role.. Surprised smile 

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 Smile ]….

 

Where should you write code ?

Use a text editor Open-mouthed smile [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

image

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 Smile 

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 !) Disappointed smile 

Advertisement

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.