Judgment expression
if.. then.. else
Expressions are one of the most commonly used foundations in programming languages. Let’s look at what expressions look like in Haskell.
Let’s look at a small example to get a feel for it (again with the help of the GHC compiler) :
Prelude> isTwo n = if n==2 then True else False
Prelude> isTwo 2
True
Prelude> isTwo 3
False
Prelude> :t isTwo
isTwo :: (Eq a, Num a) => a -> Bool
Copy the code
This is a very simple if.. then.. Else expression, isTwo is a function, n is an input parameter; As you can see, Haskell expressions are not wrapped in parentheses as they are in JS;
Of course, you can also write something like the equal operator in JS;
Prelude> isFive = (==5)
Prelude> isFive 5
True
Prelude> :t isFive
isFive :: (Eq a, Num a) => a -> Bool
Copy the code
Another big difference from JS is that Hskell’s if.. then.. Else expressions after else cannot be omitted;
That is, you must define the value that will be returned if the condition is true and the value that will be returned if the condition is not true, and both must return the same type, thus ensuring the integrity of the function definition to some extent.
In fact, if.. then.. Else is a structural expression that can also be understood as an operator. It belongs to: the mixed-position operator;
Ordinary addition, in the middle of two arguments, is called the infix operator;
Function, which precedes an argument, can be understood as: prefix operator;
The idea of “input => compute => value” in functional programming is everywhere;
switch
If else, switch:
Prelude> :{
Prelude| week n = case n of
Prelude| 1 -> "Mon"
Prelude| 2 -> "Tues"
Prelude| 3 -> "Wed"
Prelude| 4 -> "Thurs"
Prelude| 5 -> "Fri"
Prelude| 6 -> "Sat"
Prelude| 7 -> "Sun"
Prelude| _ -> error "invalid"
Prelude| :}
Prelude> week 4
"Thurs"
Copy the code
There is no need for the break keyword in Haskell. When it matches a condition, it automatically jumps out.
The _ underscore defines other default conditions;
Pattern matching
There is another way to express conditional operations — pattern matching;
Prelude> :{
Prelude| abs4 n
Prelude| | n > 4 = n
Prelude| | otherwise = -n
Prelude| :}
Prelude> abs4 2
-2
Prelude> :t abs4
abs4 :: (Ord p, Num p) => p -> p
Copy the code
| separate function parameters according to the specific conditions.
In pattern matching, more accurate and more directional patterns are always placed before relatively general and broad patterns (priority matching).
Bengua thinks that the pattern matching here is somewhat similar to the chain of responsibility mode. Match in order, put the judgment of the condition that is more likely to be correct in the first place, give priority to the execution of the judgment, and jump out immediately if the condition is met.
JS to encapsulate implementation responsibility chain, however, Haskell directly support native grammar, | is on an if.. then.. Else encapsulation;
The operator
Above already mentioned: plus, minus, etc., these essence and function are the same, function is also an operator, plus and minus is also a function!
You can print the type on the GHC console to see:
Prelude> :t (+)
(+) :: Num a => a -> a -> a
Prelude> :t (-)
(-) :: Num a => a -> a -> a
Copy the code
They just belong to operators in different positions (prefix, infix, suffix, mixed position);
In fact, the operator has three attributes:
-
- Priorities (in Haskell, there are ten priorities (0 to 9));
-
- Binding (divided into left binding, right binding, no binding);
-
- Position (front, middle, back, mixed);
Provide a table of priorities and associativity:
Image credit: Asynchronous Community
Like operators!! : fetch the NTH element from a list (starting from 0)
Prelude > [1, 2, 3, 4, 5]!!!!! 1 2Copy the code
Another example is mod: mod
Prelude> mod 7 2
1
Copy the code
There is one important operator to note: $
Prelude> :t ($)
($) :: (a -> b) -> a -> b
Copy the code
What is it for?
When you want to define f (g (h x)), you can abbreviate it to f $g $h x, so that successive calls to the function are lighter and easier to read;
Prelude> let f1 = (*2)
Prelude> let f2 = (+1)
Prelude> f1 $ f2 7
16
Copy the code
Compose is called from right to left, which explains why compose is changed from right to left.
$, $! , $!!!!! Behind there are big uses (lazy evaluation), first here for a simple understanding;
summary
Haskell Haskell
- How to write if else, how to write JS;
- How to write switch and JS;
- Pattern matching (similar to chain of responsibility pattern);
- Functions are equivalent to operators (very important);
- Three properties of the operator;
- Special operators such as:
!!!!!
,$
And so on;
These are the basics of Haskell functional programming, and the magic of using functions as computations. Even if you can’t use Haskell in production, it’s useful for your daily programming thinking. I hope you enjoy it
The above.
I am Anthony Nuggets, the public account of the same name, output exposure input, technical insights into life, goodbye ~