Haskell Study Note(Lesson 1 - 3)
Haskell Study Note(Lesson 1 - 3)
本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/162314396

Lesson 1 : What is Haskell
Haskell is a Pure Functional Programming Language
Every function in haskell is pure funciton, and it has no Side Effects ( Immutability )
Online Haskell Compilers & Platforms
To use Haskell, it need GHC. But it is not very convenient, so I use online haskell compilers. Here are the two that I think is excellent:
Lightweight interface : https://www.jdoodle.com/execute-haskell-online
Coding Practice : https://www.codewars.com
In fact, if you only need an online haskell runner, you can just open any online judge (like codeforces, atcoder or luogu), then select a single problem and click the online ide, choose the language “haskell”, and you can test your code easily !
Hello World!
Every progamme in haskell, if it want to communicate with user, it need IO, like :
1 | main :: IO() |
Types
in haskell, it has this basic type:
Basic Value Types
Integral type:
-
Int : Bounded and signed interger
-
Word : Bounded and unsigned interger
-
Integer : unbounded ans signed interger
Floating-point type:
- Float/Double : floating point
Text/Character Types
-
Char : Single Unicode character (UTF-32)
-
String : linked list of chars
Boolean
- Bool
Operator and Expression
-
+: Addition -
-: Subtraction -
*: Multiplication -
/: Floating division (returns Fractional) -
div: Interger floor division (Eg:7 div 2 = 3) -
mod: Modulo -
^: Exponentiation
Practice: "Greeting and Adder"put
1 | -- this is my first haskell coding programme |
My Coding Note:
-
If you want to do more thing in main, you need:
main = doand Tab -
Cautious about how to spell
putStrLn -
To show number in string, you need
show()to convert -
Comment : you can use
--
Lesson 2: Basic Values & Simple Operations
To see the type
-
If you are in the terminal, you can just use :
:t 42.
And it will returnInt -
But if you are in the file
.hs, we need to importData.Typeable:
1 | import Data.Typeable (typeOf) |
And it seems stupid…
String Concatenation
++: Concatenate strings, and it can only use in String
Comparison
It will return True or False
-
==//=: Equal to or Not equal to -
>/<: Greater than and Less than -
>=/<=
The equality types can also use in Lists and Tuples
Operator Precedence
-
^Exponentiation (Right-assoc) -
* div / modMuliple/Devison/Module (Left-assoc) -
+ -Plus/Minor -
:List cons -
== /= < <= > >=Comparison operators -
&&Logical AND -
||Logical OR -
.Function composition
Practice : “Simple Calculator Mini Program”
1 | totalPrice :: Double -> Double -> Double |
Note:
-
In haskell, every first character of function and variable should not be uppercase letter , but the character in the middle or last is legal to use uppercase letter.
-
If you want to say a constant is a float-point type,
100is prohibited, and100.0is legal
Lesson 3: Function
Basic Structure of a Function
a function is devided into 2 part:
- Type Signature
1 | functionName :: InputType -> OutputType |
- Function Body
1 | functionName pareameter = expression |
Parameters and Return Values
-
Parameters
The parameters are put after the function name, and every parameter is linked to an input -
Return Values
In haskell, there is noreturn. So the expression itself is the output -
Multi-paramter Functions
1 | functionName :: Type1 -> Type2 -> Type3 -> ReturnType |
To use this function, you can just functionName 1 2 3
Where: Local Names
The basic use of where :
1 | functionName param = expression |
For example:
1 | sumOfsquares :: Int -> Int -> Int |
Function compositon
To use the output of the first function as the input of the second funciton
Basic use:
1 | (f . g) x = f (g x) |
For example:
1 | square :: Int -> Int |
Practice: Unit Converter
1 | convertor :: Double -> Double |