About isPrefixOf in haskel

Asked 2 years ago, Updated 2 years ago, 65 views

In Haskell, I learned that [1,2,3] is the sugar coating syntax for 1:2:3:[].

import Data.List
isPrefixOf(1:[]) (1:2:3:[])
-- True

However, strictly speaking, 1:[] does not appear to be prefix.
1:[]:2:3:[] sounds like a prefix, but what should I think that this is not an interpretation of [1:2:3]?
I don't think either the implementation or the sugar coating syntax in the list in is accurate.

haskell

2022-09-30 15:52

2 Answers

Note: The original question has been significantly "edited" by a third party, making it difficult to say that the answer is the answer to the current question.The original question had two questions:

  • Misunderstanding questions about the difference between sugar-coated syntax and character development
  • Questions about
  • isPrefixOf sentences describing prefix as "list included" (unknown exhibit)

I think it's a reasonable question and an answerable one.

Sugar-coated syntax is not a string replacement of expressions like macros in C language, but should be considered with appropriate parentheses to avoid damaging the structure of expressions.

isPrefixOf[1][1,2,3]

It is wrong to literally deploy isPrefixOf1:[] 1:2:3:[].Correctly complemented parentheses,

isPrefixOf(1:[]) (1:2:3:[])

Equivalent to

isPrefixOf is a function that looks at the list element of the first argument from the beginning to see if it appears in the same order from the beginning of the list of second arguments.It does not examine that the list structure of the second argument contains the same list.I don't know what "included" means, but I don't think it's possible that the explanation is misunderstood because it's not very accurate.

If anything, the description may be more appropriate for isSuffixOf:

isSuffixOf[3,4][1,2,3,4]

is True, but the formula for expanding the sugar coating syntax,

isSuffixOf(3:4:[]) (1:2:3:4:[])

If you look at the , you can see that the list structure for the first argument is included in the structure for the second argument.


2022-09-30 15:52

The appearance of (:) is left and right, and the parentheses are omitted on the premise of the right connection, so I thought I didn't understand it anymore.

A list is conceptually this data structure.

 data List x = Nil | Pair x (List x)

A list List x with elements of type x is either an empty list (as indicated by Nil), or a pair of elements themselves and List x.The end is always Nil (if there is an end).

Nil

and an empty list.

Pair1Nil

is a list of length 1s with "1" as one element.
Therefore, this is what a list of 1, 2, and 3 looks like.

Pair1 (Pair2(Pair3Nil))

The last Nil is not an element of the list, it just represents the end of the list.If you write this in a normal list notation, it means [1,2,3], and Nil appears nowhere.

Now,

  • Write Pair(x,y) as x:y
  • Nil is written as []
  • Intermediate operator (:) means right-coupled, that is, x:y:z means x:(y:z)

If you decide to replace the example above,

  • [] is an empty list
  • 1:[ ] lists length 1 with only element 1
  • 1:(2:(3:[])) is a list of 1,2,3 lengths 3.1:2:3:[]
  • if you omit parentheses using the coupling law.

will be

On the other hand, as a syntactic sugar coat, it is useful to write [1] a list of length 1 with only element 1 and [1,2,3] a list of length 3 with element 1, 2, and 3.If you compare them,

  • [1,2,3] <1:2:3:[]

I think you can see that there is such a response.Note that [] was not "added" at the end.

Now, isPrefixOf can be defined as follows:

isPrefixOfNil_=True
isPrefixOf(Pair ab)Nil=False
isPrefixOf(Pair ab)(Pair cd) = a==c&&isPrefixOf bd

In other words, if the first argument is Nil first, it is true.If the second argument runs out first, it's false.If both are paired, compare the current elements, and if they are equal, compare the remaining elements.

Replace the example in the question with this notation:

isPrefixOf(Pair1Nil) (Pair1(Pair2(Pair3Nil)))

Follow the instructions.

By the way, I can of course think of a list that includes Nil as an element.Since integers and lists are not mixed, consider the list of lists.For ease of viewing

 x1 = Pair1Nil -- List of length 1 consisting of only element '1'
x2 = Pair2Nil -- List of length 1 consisting of only element '2'

Defined as

Pair x1 (Pair Nil (Pair x2 Nil))

You can make a list like this.This is all the same when deployed:

Pair(Pair1Nil)

Replace with (:) and []:

 (1:[]):[]:(2:[]):[]

If only the inner list is sugar-coated syntax:

 [1]: [ ]: [2]: [ ]

Put everything in sugar-coated syntax:

[1], [], [2]]

That's right.


2022-09-30 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.