Haskell notes (ii) List and tuples

Source: Internet
Author: User
Tags zip
The simplest of functions

Functional programming, first of all to have a function bar, to see the simplest function, the first is the function name, followed by the input variable, the expression is the output

Doubleme x=x+x
Haskell's function has no return statement, the last word is the natural return value, the function is saved as a file, and the file ends with a. HS.

Read well-written files (end with. HS)

: L file name

List Data Structure List Basic Operations The type of each element in the list needs to be consistent using let XXX = [1,2,3,4,5] To declare the string is also a list use: Add a new element, such as 5: [1,2,3,4] Add 5 to the list, can be added before or after use!! To get the data for the list index, such as [1,2,3,4]!! 2 The result is 2 head returns the first element, tail returns the last element except the first one thought, and Init returns the other element in addition to the last element. Length returns a list of NULL checks if the list is empty, returns true for null Reverse reverses the first few elements of the list take 2 [1,2,3,4] drop deletes the first few elements maximum returns the list maximum, Minmum returns the minimum sum sum elem determines whether the element is in the list, Use infix expression 4 \elem ' [1,2,3,4] ' range operation using: To generate a large number of contiguous data, such as [1..100] can use the first two elements to represent the step, such as [1,3...100] generate 100 in the odd Haskell support an infinite list, such as [1,2..],haskell is lazy language, only when run to evaluate the value, So the infinite sequence cycle is used to loop a list, generating an infinitely long list, such as Cysle [+.], which will generate [1,2,3,1,2,3.] Repeat is used to repeat a data and generate an infinite list, such as repeat 3 will generate [ 3,3,3,3,3.] list inclusion and filtering

Haskell is a functional programming language, so there are some things that we've done differently in common programming languages, and Haskell is closer to mathematical descriptions, like comprehension in a mathematical set.

The list can be written like this:

[x*2 | x <-[1..10]]

This list will return [2,4,6,8,10,12,14,16,18,20], obviously, | The left side is the expression, the right is the set, the set [1..10] is assigned to X, and then the x*2 operation, resulting in a new list.

For the above formula, we can also modify, add some constraints, this is called filtering

[x*2 | x <-[1..10], x*2 >= 12]

In this way, the return is [12,14,16,18,20], only the expression >=12 will output some real

Okay, let's get a complex, write a function named TestFunc, find all the odd numbers that are greater than 10 and divide by 3 by 2 in the input set, and multiply the odd numbers that will be found by 5.

TestFunc abc=[x*5 | x<-abc,x>10,x ' mod ' 3==2,odd x]

OK, save this as a. HS end of File FUNC.HS. Then open the GHCI terminal, enter: L func, run TestFunc [20..60], the result will output [115,145,175,205,235,265,295], how, simply put, you want to use the command-type programming language (such as C language) to implement how this will be written. Oh

Well, a little more difficult, to find all the primes in a given sequence, first think of C language how to write, I write a Haskell, in order to look good, I put a line of content into a few lines.

Primenum innum=[x| x<-innum,
                    odd X, 
                    sum[1|_<-[y|y<-[2..x-1],
                                x ' mod ' y = = 0]
                        ]==0
                ]

At the command line, enter:

Primenum [3..100]

The output is:

[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

Yes, just one line, let me explain, innum is the input sequence, first determine whether it is odd, and then from 2 to x-1 to regenerate a sequence assignment to _,_ that we do not care what the value is, and then x modulo y if equals 0, output a 1, otherwise output, the output of a new sequence sum, if and equal to 0, Indicates that no x modulo y equals 0, indicating that this is a prime number, then the x is output. It sounds like a bit of a detour, and you'll see that functional programming is closer to our thinking and math models than the command line. tuple (tuple) data structure

In a sense, a tuple (tuple) is much like list–, which is a container for storing multiple values in an individual. But they are fundamentally different, a list of numbers is a set of numbers, they are of the same type, and do not care about the number of elements contained therein. A Tuple requires you to be very specific about the number of data that needs to be combined, depending on the number of items and their respective types. The items in a Tuple are enclosed in parentheses and separated by commas. Another difference is that the items in a tuple do not have to be of the same type and can be stored in a tuple with a combination of multiple types. FST returns the first element of the tuple FST (8,11), returns 8 snd the last element of the tuple is returned by the zip function, the tuple list is generated through two lists, 22 integrates, such as zip [three-to-three] [4,5,6] will be generated [(1,4), (2,5), (3,6)] The list of different lengths is based on the short list by Zip combination .

We have a request, in each side is less than 100 triangle, find all right triangle, and list the length of three sides, first of all, you still think in C language how to write this program, I still have a Haskell.

[(A,B,C) | a<-[1..100],b<-[1..a],c<[1..b],c^2+b^2==a^2]

Well, there's no need for a function to get it done.

This tutorial refers to the Haskell Fun Guide, which is a learning note for this guide, but has joined some of my own ideas and summaries, first of all, thanks to the author of the Haskell Guide bonus and the mainland translator Fleurer and Taiwanese translator MnO2, thank you for your previous work.

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.