Is there a way to keep a variable from changing inside recursion in haskell?

I am trying to define the isInfixOf function, and I came up with a solution (I hope that it works well :-)),
but due to the haskell recursive nature, I wrote another (help) function that almost duplicates the code of the isInfixOf function.

this is the code:



myIsInfixOf :: (Eq a) => [a] -> [a] -> Bool myIsInfixOf [] [] = True myIsInfixOf [] list = True myIsInfixOf list [] = False myIsInfixOf listA listB | helpFunction listA listB == True = True | otherwise = myIsInfixOf listA (tail listB)

helpFunction :: (Eq a) => [a] -> [a] -> Bool helpFunction [] [] = True helpFunction [] list = True helpFunction list [] = False helpFunction (x:xs)(y:ys) | x == y = helpFunction xs ys | otherwise = False

, myIsInfixOf, .
helpFunction, myIsInfixOf , , myIsInfixOf .
helpFunction , ( ).

, - myIsInfixOf, ?

.: -)

+3
4

, . "" , . , . myIsInfixOf :

myIsInfixOf :: (Eq a) => [a] -> [a] -> Bool
myIsInfixOf (a:as) [] = False
myIsInfixOf listA listB | helpFunction listA listB = True
                        | otherwise =  myIsInfixOf listA (tail listB)

, helpFunction, - .

, , listB, listA listB. , , , , . , listA - helpFunction, , , helpFunction myIsInfixOf.

myIsInfixOf, , any tails. helpFunction - , :

helpFunction (x:xs) (y:ys) = (x == y) && helpFunction xs ys
+4

, 1) , [] list [] [], ; 2) helpFunction listA listB == True helpFunction listA listB.

myIsInfixOf :: (Eq a) => [a] -> [a] -> Bool
myIsInfixOf [] list = True
myIsInfixOf list [] = False
myIsInfixOf listA listB | helpFunction listA listB = True
                        | otherwise =  myIsInfixOf listA (tail listB)

helpFunction :: (Eq a) => [a] -> [a] -> Bool
helpFunction [] list    = True
helpFunction list []    = False
helpFunction (x:xs)(y:ys) | x == y = helpFunction xs ys
                          | otherwise = False

, .

. , . . helpFunction myPrefixOf. myIsInfixOf.

+2

sth, :

import Data.List

myIsInfixOf xs ys = any (myIsPrefixOf xs) (tails ys)

myIsPrefixOf [] _ = True
myIsPrefixOf (x:_) [] = False
myIsPrefixOf (x:xs) (y:ys) = x == y && myIsPrefixOf xs ys

, :

  • ""
+2

, "" "" , , . , ""?

: at-patterns:

myIsInfixOf listA@(x:xs) listB@(y:ys) = ...

This allows you to refer to the entire first argument as listA, the first element of listA as x and the tail as xs (the same for the second argument). Is that what you meant?

0
source

Source: https://habr.com/ru/post/1729079/


All Articles