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 EffectsImmutability

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
2
main :: IO()
main = putStrLn "Hello, World!"

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

  1. Char : Single Unicode character (UTF-32)

  2. String : linked list of chars

Boolean

  1. Bool

Operator and Expression

  1. + : Addition

  2. - : Subtraction

  3. * : Multiplication

  4. / : Floating division (returns Fractional)

  5. div : Interger floor division (Eg: 7 div 2 = 3 )

  6. mod : Modulo

  7. ^ : Exponentiation

Practice: "Greeting and Adder"put

1
2
3
4
5
6
7
8
-- this is my first haskell coding programme

main :: IO()
main = do
putStrLn "Hello ! I am zhangxixi!"
let a = 10
let b = 20
putStrLn ("The sum of a + b is: " ++ show(a + b))

My Coding Note:

  1. If you want to do more thing in main, you need: main = do and Tab

  2. Cautious about how to spell putStrLn

  3. To show number in string, you need show() to convert

  4. Comment : you can use --

Lesson 2: Basic Values & Simple Operations

To see the type

  1. If you are in the terminal, you can just use : :t 42 .
    And it will return Int

  2. But if you are in the file .hs , we need to import Data.Typeable :

1
2
3
4
5
import Data.Typeable (typeOf)

main :: IO()
main = do
print $ typeOf(42 :: Int)

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 / mod Muliple/Devison/Module (Left-assoc)

  • + - Plus/Minor

  • : List cons

  • == /= < <= > >= Comparison operators

  • && Logical AND

  • || Logical OR

  • . Function composition

Practice : “Simple Calculator Mini Program”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
totalPrice :: Double -> Double -> Double
totalPrice price numbers = price * numbers

Hasadiscount :: Double -> Bool
Hasadiscount price = price > 100.0

main :: IO ()
main = do
let pri = 51.0
let num = 2
let tot = totalPrice pri num
let had = hasadiscount tot
putStrLn("The total Price is :" ++ show(tot))
putStrLn("Have a discount? " ++ show(had))

Note:

  1. 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.

  2. If you want to say a constant is a float-point type, 100 is prohibited, and 100.0 is legal

Lesson 3: Function

Basic Structure of a Function

a function is devided into 2 part:

  1. Type Signature
1
functionName :: InputType -> OutputType
  1. Function Body
1
functionName pareameter = expression

Parameters and Return Values

  1. Parameters
    The parameters are put after the function name, and every parameter is linked to an input

  2. Return Values
    In haskell, there is no return . So the expression itself is the output

  3. 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
2
3
functionName param = expression
where
localName = localExpression

For example:

1
2
3
4
5
sumOfsquares :: Int -> Int -> Int
sumOfsquares x y = lx + ly
where
lx = x * x
ly = y * y

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
2
3
4
5
6
7
8
square :: Int -> Int
square x = x * x

addTwo :: Int -> Int
addTwo x = x + 2

squareThenAddTwo :: Int -> Int
squareThenAddTwo = addTwo . square

Practice: Unit Converter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
convertor :: Double -> Double
convertor kmm = kmm * cst
where
cst = 0.621371

changeTwoDigits :: Double -> Double
changeTwoDigits x = fromIntegral(round(x * 100)) / 100

finalAnswer :: Double -> Double
finalAnswer = changeTwoDigits . convertor

main :: IO()
main = do
let a = 10.0
let b = finalAnswer a
putStrLn ("The ans is " ++ show b)