series

  • Basic grammar for notes in the Haskell Guide to Fun Learning
  • Notes in the Haskell Guide to Fun Learning
  • Function of notes in the Haskell Guide to Fun Learning
  • Haskell’s Guide to Fun Learning notes of higher order functions
  • Module of notes in the Haskell Guide to Fun Learning
  • Custom types of notes in the Haskell Guide to Fun Learning
  • I/O notes from the Haskell Guide to Fun Learning

preface

  • Haskell is purely functional (cannot assign, references transparent)
  • Haskell is lazy
  • Haskell is statically typed
  • Haskell supports type inference
  • :l myfunctionsMyfunctions.hs can be loaded in the same directory and can be repeated
  • :set prompt "ghci> "You can change a prompt
    • : set prompt "lambda"You get lambda
    • Try writing this sentence in ~/. Ghci
  • PutStrLn can print a Chinese string

Chapter 1

  • 5 * 3To be written5 * (3)Otherwise, an error is reported
  • Boolean uses True and False
  • 1 + '1'Complains,5 + 0.4No error, because 5 can also be considered 5.0

function

  • Haskell has some infix functions, such as * and +
    • As long as you name a function with special characters, it is automatically treated as an infix function.
  • Haskell mostly belongs to prefix functions such as
    • succ 8Is take the subsequent
    • min 1 2Is the smallest
    • max 1 2Is the most
    • div 9 3Is division
  • Function calls have the highest priority,succ 9 * 10Equivalent to(succ 9) * 10
  • Prefixes can change to infixes:div 9 3You can change to9 `div` 3
  • One parameter functiondoubleMe x = x + x
  • Two parameter functiondoubleUs x y = x * 2 + y * 2
  • Function definitions have no concept of order
  • Conditional statementsif <condition> then <exp> else <exp>We can’t omit else
  • Conditional statements are expressions, not statements, and always return values
  • The function name can contain'Character, generally with'The closing function is the non-lazy evaluation version, also known as strict evaluation
  • Function names cannot begin with an uppercase letter, because uppercase letters are types
  • Functions that have no arguments are called definitions or names, such asa = 1

The list of

  • The list is a data structure of homogeneous, which can only store elements of the same type but cannot store elements of different types
  • A = [1, 2, 3]
  • use++You can concatenate two lists, but it iterates over the first list
  • "hello"['h','e','l','l','o']The syntactic sugar
  • Use the Cons operator:You can insert an element in the head of the list1: [2, 3]
  • [1, 2]2-1: []The syntactic sugar
  • Get values by index can be used!!!!!Symbol:[1, 2, 3]!!!!! 0The value is 1, and an error is reported if the index is out of bounds
  • > [2, 1, 0]Value is True
  • More list operations:The head/tail/last/init/length/null/reverse/task 3 / drop 3 / maximum/minimum sum/product quadrature
  • head []An error
  • 4 ` elem ` (three, four, five)Value is True
  • [20] 1..Generate a list of 20 numbers from 1 to 20
  • ['a'..'z'] / ['K'..'Z']
  • [20] 2, 4..Generate [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
  • 1] [20..I get an empty list, so I should write[1] 20, 12..
  • take 24 [13, 26..]
  • cycle [1, 2, 3]Would loop through the list indefinitely, getting 1,2,3,1,2,3,1,2,3… List of components
  • repeat 5I get an infinite list of 5’s
  • repeat 3 10Get,10,10 [10]

List derivation

  • [x* 2 | x <- [1.. 10]]Get [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
  • [x* 2 | x <- [1.. 10], x>5, filter2, filter3]You can filter x
  • [ x* y | x <-[1, 3, 5], y <- [2, 4, 6]]Get a list of length 9 [2,4,6,6,12,18,10,20,30]
  • [ x* y | x <-[1, 3, 5], y <- [2, 4, 6], x* y > 10]Get,18,20,30 [12]
  • length' xs = sum [1 | _ <- xs]You can use the length’ function to find the length of the list
  • Can also be nested:
    ghci> xxs = [[1, 3, 5, 2, 3, 1, 2, 4, 5],[ 1, 2, 3, 4, 5, 6, 7, 8, 9],[ 1, 2, 4, 2, 1, 6, 3, 1, 3, 2, 3, 6]]
    ghci> [ [ x | x <- xs, even x ] | xs <- xxs]
    [[2, 2, 4],[ 2, 4, 6, 8],[ 2, 4, 2, 6, 2, 6]]
    Copy the code

Tuples tuple

  • A tuple can hold different types of values, that is, it is heterogeneous
  • The length of a tuple is fixed and cannot be changed
  • (1, 3)Is a tuple,(1,'a', "hello")Is also a tuple
  • A tuple of length 2 is also called a sequence pair
  • The types of tuples and triples are considered different, so[(1, 2), (three, four, five)]An error is reported because lists can only contain things of the same type
  • The length is the same, but if the elements are of different types, the tuple types are treated as different
  • Tuples of length 1 are not allowed because they are not interesting
  • Sequence to related apis
    • FST (8, 11) = > 8
    • The SND (8, 11) = > 11
    • Zip [1,2,3] [4,5,6] => [(1,4),(2,5),(3,6)]
      • If the length of the two arguments to the ZIP is different, the extra items are discarded