Answer the question
In order to leave comments, you need to log in
How does this code work in Haskell?
Hello, I just started learning Haskell. Here I found a code that every few characters (which we set as a parameter) inserts a given character (also a parameter).
But I can't understand how it works.
func1 :: [a] -> Int
func1 [] = 0
func1 (_:xs) = 1 + func1 xs
func2 :: Int -> [a] -> [a]
func2 _ [] = []
func2 n [email protected](_:xs')
| n > 0 = func2 (n-1) xs'
| otherwise = xs
func3 :: [a] -> Int -> a -> [a]
func3 xs 0 y = xs
func3 [] n y = []
func3 xs n y
| func1 xs < n = xs
| otherwise = n `take` xs ++ [y] ++ func3 (drop n xs) n y
main :: IO ()
main = do
print(func3 "1234567" 2 '@')
func2 n [email protected](_:xs')
| n > 0 = func2 (n-1) xs'
| otherwise = xs
func3 xs 0 y = xs
func3 [] n y = []
func3 xs n y
| func1 xs < n = xs
| otherwise = n `take` xs ++ [y] ++ func3 (drop n xs) n y
Answer the question
In order to leave comments, you need to log in
Generally figured out:
func3 means that when passing 0 as the second argument, you should always return the first argument unchanged.
And when passing an empty list as the first argument, it is always an empty list.
In other cases -
If func1 xs < n (func1 calculates the length of the list), then return xs, otherwise (some tricky thing)
In func2, using @, we simply pull out the first element of the list in _ (thanks to mikeyuriev for the correction), and the tail list - in xs'
And then there is a recursive call.
As a result, func2 will discard the first n elements of the list
n `take` xs ++ [y] ++ func3 (drop n xs) n y
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question