Path to mathematics (2)-four major artifacts-Haskell (28)

Source: Internet
Author: User

24. Data. Set

Set is between list and ing. All elements are unique.

Fromlist constructs the set.

Prelude data. Set> Let X = data. Set. fromlist [11,22, 33,44

Prelude data. Set> let y = data. Set. fromlist [91,32, 33,44, 55]

Prelude data. Set>

Intersection

Prelude data. Set> intersection X Y

Fromlist [33,44, 55]

Prelude data. Set>

Difference removes some elements from the first set. These elements are handed over to the second set.

Prelude data. Set> data. Set. Difference X Y

Fromlist [11, 22]

Prelude data. Set>

Union completes the union of two sets

Prelude data. Set> data. Set. Union X Y

Fromlist [11, 22, 32, 33, 44,55, 91]

Prelude data. Set>

Null, size, Member, empty, Singleton, insert, and delete are similar to map operations.

25. definition module

We create a module for some functions defined in previous exercises:

We define two modules: mymodule. Test2 and mymodule. test1.

First, create two Program Files: test1.hs and test2.hs.

Test1.hs content:

Module mymodule. test1

(Mylength

, Oddcount

, Evencount

, Mysum

) Where

--------------

Mylength rows = sum [1 | _ <-rows]

Oddcount Limit = sum [1 | x <-limit, X 'mod' 2/= 0]

Evencount percent = sum [1 | x <-percent, even X]

-------------

Mysum: (integral a) => A->

Mysum 0 = 0

Mysum n = N + mysum (n-1)

Test2.hs content:

Module mymodule. Test2

(Myfst

, Mycnt

, Mylst

, Mymin

) Where

------------------

Myfst: (A, B, C)->

Mycnt: :( a, B, c)-> B

Mylst: (A, B, C)-> C

Myfst (A, _, _) =

Mycnt (_, B, _) = B

Mylst (_, _, c) = C

----------------------

Mymin: :( ord a) => [a]->

Mymin [] = error "empty list! "

Mymin [x] = x

Mymin (X: XS)

| X <minval = x

| Otherwise = minval

Where minval = mymin XS

Place the two files under the mymodule subdirectory of the program execution directory that requires the two modules. Then write a program to load the two modules and call the functions

Import mymodule. test1

Import mymodule. Test2

Test1_1 A = mylength

Test1_2 A = evencount

Test2_1 A = mycnt

Test2_2 A = mymin

Run the test:

* Main>: Load "mytestm. Hs"

[1 of 3] compiling mymodule. Test2 (mymodule \ test2.hs, interpreted)

[2 of 3] compiling mymodule. test1 (mymodule \ test1.hs, interpreted)

[3 of 3] compiling main (mytestm. Hs, interpreted)

OK, modules loaded: mymodule. test1, mymodule. Test2, Main.

* Main> test1_1 [11,22, 33]

3

* Main> test1_2 [11,22, 33]

1

* Main> test2_2 [11,22, 33]

11

* Main> test2_1 (11,22, 33)

22

* Main>

 

All content of this blog is original and reposted in any form is prohibited without prior written consent. Http://blog.csdn.net/u010255642

 

 

 

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.