haskell# Haskell - Weave two lists together in chunks of n size?

I am practicing some Haskell exam paper questions, and have come across the following

Define a Haskell function weaveHunks which takes an int and two lists and weaves them together in hunks of the given size. Be sure to declare its type signature.

Example:

weaveHunks 3 "abcdefghijklmno" "ABCDEFGHIJKLMNO"

=> "abcABCdefDEFghiGHIjklJKLmnoMNO"

I have found the following on Stack Overflow, which is just too weave two lists together but only in chunks of 1

```
weaveHunks :: [a] -> [a] -> [a]
weaveHunks xs [] = xs
weaveHunks [] ys = ys
weaveHunks (x:xs) (y:ys) = x : y : weaveHunks xs ys
```

I am having problems adjusting this to take chunks fo n size, I am very new to Haskell but this is what I have so far

```
weaveHunks :: Int -> [a] -> [a] -> [a]
weaveHunks n xs [] = xs
weaveHunks n [] ys = ys
weaveHunks n xs ys = (take n xs) : (take n ys) : weaveHunks n (drop n xs) (drop n ys)
```

I am getting an error on the last line

(Couldn't match type

`a' with`

[a]')

Is `(drop n xs)`

not a list?

Solution

You're very close!

By using the `:`

operator to prepend the hunks, you're expressing that `take n xs`

is one element of the result list, `take n ys`

the next, and so on. But actually in both cases it's *multiple* elements you're prepending. That's the `[a]`

that should actually be just `a`

.

The solution is to use the `++`

operator instead, which prepends an entire list rather than just a single element.

This is the full solution as I'd write it:

```
weaveHunks :: Int -> [a] -> [a] -> [a]
weaveHunks _ xs [] = xs
weaveHunks _ [] ys = ys
weaveHunks n xs ys = xHunk ++ yHunk ++ weaveHunks n xRemain yRemain
where [(xHunk, xRemain), (yHunk, yRemain)] = splitAt n <$> [xs,ys]
```

- Haskell pattern matching - what is it?
- How to understand the <- in Haskell's do notation with an uncommon ">>=" implementation in a Monad?
- TVar that blocks read until change?
- Convert the modified cursors back to a document
- Could someone explain to me what these Iterator, Yield monad types and functions mean like I am 5?
- Haskell void function
- Is Haskell a strongly typed programming language?
- How to build lens expression dynamically?
- Why is UndecidableInstances not required here, for instance C1
- How are Dynamic Programming algorithms implemented in idiomatic Haskell?
- Type-level tuple: how to avoid nesting in a type argument
- What determines what GHC (and Cabal, and HLS, ...) version is marked as "recommended" in GHCup?
- How to zip lists with different length?
- Constant function optimization
- Is it possible to properly type hint the filterM function in Python?
- How to change the base case so it starts recursion all over?
- How laziness works with applicative functors in Haskell?
- Clean and type-safe state machine implementation in a statically typed language?
- Extracting the first Just value from [Maybe a]
- A monad is just a monoid in the category of endofunctors, what's the problem?
- How to derive instances in recursion schemes
- How do I make a do block return early?
- Ambiguous occurrence `take`
- If we bind two parsers and the second parser fails, does the string get parsed once?
- Haskell list sum generalization
- Stack run single benchmark
- How to properly step through recursive parsing?
- How do I extract this text from an xml file in Haskell
- Is it possible to generate comments to functions in Template Haskell?
- Why does GHC's type-checker accept this UndecidableInstance trick?