Haskell List related Operations

Source: Internet
Author: User
Tags repetition

only elements of the same type can be stored in a list.

  + +: Merges two lists into a list. example, [1,2,3]++[4,5,6], the result is [1,2,3,4,5,6].

  : : Add the preceding element of ":" to the list after ":". The newly added element is in the head position of the list. Example, 1:[2,3,4,5,6], the result is [1,2,3,4,5,6].

   !! : Takes the element of the list subscript position. example, [1,2,3,4,5,6]!! 0, the result is 1.

  Elem: Determines whether an element exists in the list. example, Elem 1 [1,2,3,4,5,6], the result is true. infix form, 1 ' elem ' [1,2,3,4,5,6].

PS: ' is not a single quote in infix form, it is the symbol below the ' ~ ' key on the keyboard.

Second, compare list.

[3] The result of > [2,1] is true.

head: Takes out the first element of the list.

  tail: Remove all elements except the first element of the list.

  Last: Takes out the final element of the list.

  init: Takes out all elements except the last element of the list.

  Length: Returns the Long value of the list.

  NULL: Determines whether the list is empty.

  Reverse: Returns a list's inverse list.

  Maximum: Takes the maximum value in the list.

  minimum: Takes the minimum value from the list.

  sum: Calculates the sum in the list element.

  Take: Takes the first n elements of a list. example, take 2 [1,2,3,4,5,6], the result is [+].

Drop: Takes the second x element of the list. example, Drop 2 [1,2,3,4,5,6], the result is [3,4,5,6].

Three, range related

[1..10] equivalent to [1,2,3,4,5,6,7,8,9,10]

[' A ' ... ' G '] equivalent to [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ']

[3,6..10] equivalent to [3,6,9]

  cycle: Infinite repetition of subsequent content. example, take 3 (cycle []), the result is [1,2,1].

  repeat: infinite repetition of subsequent elements. example, take 2 (repeat 1), the result is [+].

  replicate: Repeats the element after N times. example, replicate 2 [[1,2],[1,2]] and the result is [[]].

Iv. representation of a set

  MoD: Take the remainder operation.

  Odd: Determine if a number is odd.

[x*2|x<-[1..10]] equivalent [2,4,6,8,10]

[X|x<-[1..10],odd x,mod x 3 = = 0] equivalent to [3,9]

Haskell List related Operations

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.