Haskell is very
different than most programming languages you will already learned, as you
can’t change the value of an identifier (so technically there are no
variables). Haskell features lazy evaluation, so methods are not called when
you might expect them to be, but instead called when the value is needed, and
this includes sequences so you can define a sequence 1 to infinity and still
process it. The symbol _ is used to represent a value that you are not
interested in.
Haskell uses
patterns, so you can define the factorial sequence as
factorial
:: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n – 1)
factorial 0 = 1
factorial n = n * factorial (n – 1)
which means factorial
is defined as taking an integer and returning an integer, that the factorial
for 0 is 1 and the factorial for any other value of n is the factorial of (n-1)
* n. As you can also see, brackets are not used (unless you need to do an
operation such as n – 1 to stop it being treated as 2 parameters n and -1.
You can define
types, which can be either limited to a fixed number of alternatives such as
data
Sex = Male | Female
Which says sex is either male or female, or you can define the equivalent of a C structure using
Which says sex is either male or female, or you can define the equivalent of a C structure using
data Person =
Person String Sex Int
Which says a person
has a string (for their name, their sex and an integer for their age).
You can define an
instance of a person using
boss
:: Person
boss = Person “Neil” Male 52
boss = Person “Neil” Male 52
Or
you can use an instance directly. You can define methods to access the
components of a Person, using
getName
:: Person -> String
getName (Person name _ _) = name
getName (Person name _ _) = name
getSex
:: Person - > Sex
getSex (Person _ sex _) = sex
getSex (Person _ sex _) = sex
getAge
:: Person -> Int
getAge (Person _ _ age) = age
getAge (Person _ _ age) = age
An example of using
an instance directly would be
map getName [Person
“Tom” Male 30, Person “Terri” Female 25, boss]
which returns
[“Tom”, “Terri”,
“Neil”]
So if functional programming in a language such as Haskell has you pulling your hair out, never
worry we have people who have already gone bald and have managed to write large
scale programs in Haskell and can save you the stress and worry that you may
have encountered. Our experts are available for you on a 24/7 basis as they are
located around the world in Europe, Australia and America and we can provide
solutions for you. The fastest response time from our experts at quoting a new
assignment is between the hours of 8AM and 11PM (PST).
You can rely on Programming Assignment Helper if you need some help with your Haskell homework help.
0 comments:
Post a Comment