Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a Function to every element in a list

Tags:

haskell

I've created a function m such that

m "abc" "def" == "bcd" 

and I would like to create another function that uses m to generate the output ["bcd","efg","hia"] when given the input ["abc","def","ghi"]

The definition of m is

m :: [a] -> [a] -> [a]
m str1 str2 = (drop 1 str1) ++ (take 1 str2)
like image 461
CamelBak Avatar asked Nov 27 '20 19:11

CamelBak


1 Answers

You can make use of zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] here where you take the entire list as first parameter, and tail (cycle l) as second parameter (with l the list):

combine :: [a] -> [a]
combine l = zipWith m l (tail (cycle l))

zipWith will enumerate concurrently on both lists and each time call m with an element of the first and the second list. For example:

Prelude> combine ["abc","def","ghi"]
["bcd","efg","hia"]
like image 114
Willem Van Onsem Avatar answered Oct 08 '22 08:10

Willem Van Onsem