Function (Haskell)

From StudyWiki

Jump to: navigation, search


This page is about functions in Haskell. For other languages, and general information, see Functions.


Contents

Function Definition

Function Definition

   <function-name> :: <input type> -> <output type>
   <function-name> <pattern> = <expression>

Function Definition with Multiple Inputs

   <function-name> :: <input type 1> -> ... -> <input type n> -> <output type>
   <function-name> <pattern 1> ... <pattern n> = <expression>


<function-name>
the name of the function, used to call it within the program
<input type>
the expected type of the input
<output type>
the type that the function outputs
<pattern>
A pattern to match to the given inputs that is used to identify the arguments within the function and <expression>
<expression>
the expression to evaluate that gives the function's result


Functions are calculated by expanding their definitions with the supplied values and evaluating the resulting expression.


Patterns Within Function Definitions

  • Variables: x, y, z - use for any type
  • Constants: 0, 'a', True, "string" - definition of specific cases
  • Pairs: (a,b) - is the argument is a pair type
  • Tuples: (x, ..., z) - is the argument is a tuple type
  • Wildcards: _ - the underscore matches all input and can be used when the output doesn't use the input


Guarded Functions

Guarded Function Definition

   <function-name> :: <input type 1> -> ... -> <input type n> -> <output type>
   
   <function-name> <variable 1> ... <variable n> 
       |<guard 1> = <expression 1>
       |...
       |<guard m> = <expression m>


<function-name>
the name of the function, used to call it within the program
<input type>
the expected type of the input
<output type>
the type that the function outputs
<variable>
the name given to variables within the function and <expression
<expression>
the expression to evaluate that gives the function's result
<guard>
a Boolean condition that determines the expression to evaluate by selecting the first correct guard


Instead of matching patterns, guarded functions test a boolean condition. A guarded function has a number of guards that each test for a different condition. The expression that corresponds to the first guard that is correct is evaluated.

Otherwise

  • used with guards to catch all undefined situations.
  • for example:
   | otherwise = error "error!"

Calling Functions

Functions can be called by using the function name followed by the input values. If there are values of different types, the arguments must be supplied in the same order as their are defined in the function definition.

e.g.
functionx 1 2 3

Functions can also be called "infix" by using the backquote character (`) e.g. 10 `mod` 9

Local Declarations

  • when a function is required more than once within a single function, local declaration can be used
  • avoids
    • cluttering top-level environment with definitions
    • inefficient to re-evaluate same expressions
  • use where keyword to define function within another function.

E.G.

   sumSquares :: Int -> Int -> Int
   sumSquares x y = sq x + sq y
                    where sq z = z * z

The where keyword can be followed by a list of local declarations.

Personal tools