In Haskell, **prefix functions** are functions that are written **before** their arguments, as opposed to **infix functions**, which are written between their arguments. Most functions in Haskell are prefix by default.

### Key Points About Prefix Functions in Haskell

**Syntax**: In prefix notation, the function name comes**before**the arguments. This is the most common form of function application in Haskell.- Example:
`add 2 3`

(where`add`

is the function, and`2`

and`3`

are the arguments).

- Example:
**Usage**: Prefix notation is used with most functions, especially those with descriptive names, making code readable and closer to natural language.

Example:

```
sqrt 16 -- Result: 4.0
max 10 20 -- Result: 20
```

3. **Higher-Order Functions**: Functions like `map`

, `filter`

, and `foldr`

are typically used in prefix form, as they take other functions or lists as arguments and operate on them in sequence.

Example:

```
map (+1) [1, 2, 3] -- Result: [2, 3, 4]
filter even [1, 2, 3] -- Result: [2]
```

4. **Prefix Functions vs. Infix Operators**: Operators, like `+`

and `*`

, are usually used in infix form. However, you can also use infix operators in prefix form by enclosing them in parentheses:

Example:

```
(+) 2 3 -- Result: 5
(*) 4 5 -- Result: 20
```

**5. Using Infix Functions as Prefix**: Any function that’s defined as infix can be used as prefix by enclosing it in parentheses, which allows for flexible function application styles.

Example:

```
let subtract x y = x - y
subtract 10 5 -- Result: 5 (prefix form)
```

### Example of Prefix Function Usage

**Example 1: Basic Prefix Functions**

```
-- Using standard functions in prefix notation
max 5 10 -- Result: 10
abs (-7) -- Result: 7
```

**Example 2: Higher-Order Functions in Prefix Notation**

```
-- Using map, a higher-order function, in prefix notation
map (*2) [1, 2, 3] -- Result: [2, 4, 6]
```

**Example 3: Infix Operators as Prefix**

```
-- Using `+` as a prefix function
(+) 3 4 -- Result: 7
```

In summary, **prefix functions** are the standard way of writing functions in Haskell, where the function name precedes its arguments. This notation is natural for most functions, especially descriptive ones, and is used for both built-in and custom functions.

## Leave a Reply