In Haskell, functions are defined using the keyword "let" followed by the function name, a list of arguments, an equal sign, and the function body. The function body specifies what the function does when applied to its arguments. Here's the general syntax:
let functionName arg1 arg2 ... = functionBody
The arguments are separated by spaces, and the function body can be any valid Haskell expression. For example, let's define a function called "square" which takes one argument and returns the square of that number:
let square x = x * x
Here, "square" is the function name and "x" is the argument. The function body is "x * x", which calculates the square of "x" by multiplying it by itself.
How to declare a pure function in Haskell?
In Haskell, you can declare a pure function by defining the function signature and its implementation. Here's the syntax to declare a pure function in Haskell:
1 2 |
functionName :: Type1 -> Type2 -> ... -> ReturnType functionName arg1 arg2 ... = functionBody |
Here's an example of declaring a pure function sumSquare
that takes two integers and returns the sum of their squares:
1 2 |
sumSquare :: Int -> Int -> Int sumSquare x y = (x * x) + (y * y) |
In this example, the function sumSquare
takes two arguments x
and y
of type Int
and returns an Int
. The function body simply calculates the sum of the squares of x
and y
.
Pure functions are referentially transparent, meaning that their output is solely determined by their input and they don't have any side effects. Consequently, they always produce the same output for the same input.
How to declare a function in Haskell?
To declare a function in Haskell, you can use the following syntax:
1 2 |
functionName :: argType1 -> argType2 -> ... -> returnType functionName arg1 arg2 ... = functionBody |
Here's an example of declaring a simple function that adds two numbers:
1 2 |
add :: Int -> Int -> Int add x y = x + y |
In this example, the function add
takes two arguments of type Int
and returns an Int
. The function body is x + y
, which adds the two arguments together.
You can call this function by providing the appropriate arguments:
1 2 3 4 |
main :: IO () main = do let result = add 3 4 putStrLn $ "Result: " ++ show result |
In this example, add 3 4
returns 7
, which is then printed to the console.
Note: Haskell is a statically typed language, so you need to specify the types of function arguments and the return type.
How to define a function with default arguments in Haskell?
In Haskell, you can define a function with default arguments by using the concept of currying. Here's an example of defining a function with default arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
-- Define a function with default arguments myFunction :: Int -> Int -> Int -> Int myFunction x y z = x + y + z -- Call the function with default arguments result1 = myFunction 1 2 3 -- Output: 6 -- Define a partially applied function with default arguments myPartialFunction :: Int -> Int -> Int myPartialFunction = myFunction 1 -- Call the partially applied function with default arguments result2 = myPartialFunction 2 3 -- Output: 6 |
In this example, myFunction
is a function that takes three Int
arguments and returns their sum. The values of x
, y
, and z
can be provided explicitly when calling the function.
You can also define a partially applied version of myFunction
called myPartialFunction
by providing a default value for x
. Since x
has a default value of 1
, you only need to provide values for y
and z
when calling myPartialFunction
.
Note that Haskell functions are normally defined without default arguments. If you need a function with default arguments, it is usually recommended to use currying to achieve the same effect.