Summary-Haskell-3] Haskell Common Data Structures

Source: Internet
Author: User
Haskell Common Data Structure Table of Contents
  • 1. Haskell basic knowledge
  • 2 Haskell important data structure-list
  • 3 Haskell Common Data Structures
    • 3.1 What is tuple?
    • 3.2 pair
      • 3.2.1 what is pair?
      • 3.2.2 pair-related functions
    • 3.3 Use tuple to write programs
1 Haskell basic knowledge 2 Haskell important data structure-list3 Haskell Common Data Structure 3.1 What is tuple

Tuple is a binary group, triple, etc. The elements in the group can be of different types, and the list element must be of the same type.

ghci>(1,2)(1,2)ghci>('h','i','j')('h','i','j')ghci>(1,'a',3.5)(1,'a',3.5)

The following example shows that the element types in the tuple determine the tuple type, that is, () and ('A', 'B') are both binary groups, but not the same type (1, 2) and (1, 2, 3) Although the element type is the same, but the number is different, it is not the same type

ghci>[(1,2),(3,4),(5,6)][(1,2),(3,4),(5,6)]ghci>[(1,2),('a','b')]errorghci>[(1,2),(3,4,5)]error
3.2 What is pair3.2.1

Pair is a binary group.

ghci>(1,2)(1,2)ghci>(3,4)(3,4)
3.2.2 pair-related functions
  • FST/SND
ghci>fst (2,3)2ghci>snd (2,3)3
  • Zip
ghci>zip [1,2,3] ['a','b','c'][(1,'a'),(2,'b'),(3,'c')]
ghci>zip [1..] ["Apple","Moto","Nokia"][(1,"Apple"),(2,"Moto"),(3,"Nokia")]
3.3 Use tuple to write programs

Example 1 Constraint Programming: Find All right triangles with three sides within [1, 10]

ghci>[(a,b,c) | a <- [1..10], b <- [1..a], c <- [1..b], b^2 + c^2 == a^2][(5,4,3),(10,8,6)]

Example 2 tuple and pattern matching

first (x,_,_) = xsecond (_,y,_) = ythird (_,_,z) = z
ghci>first ('a',3,6.7)'a'ghci>second ('a',3,6.7)3ghci>third ('a',3,6.7)6.7

Example 3

sumPair xs = [x + y| (x,y) <- xs ]
ghci>sumPair [(1,2),(3,4),(5,6)][3,7,11]
sumPair' xs = [z | (x,y) <- xs, let z = x + y]

Note: cannot be directly written as Z = x + y

ghci>sumPair' [(1,2),(3,4),(5,6)][3,7,11]

Example 4

zip' _ [] = []zip' [] _ = []zip' (x:xs) (y:ys) = (x,y):zip' xs ys
ghci>zip' [1..] ["Apple","***","MI"][(1,"Apple"),(2,"***"),(3,"MI")]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.