Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Learn You a Haskell shows the intersperse function:

ghci> intersperse '.' "MONKEY"  
"M.O.N.K.E.Y"  
ghci> intersperse 0 [1,2,3,4,5,6]  
[1,0,2,0,3,0,4,0,5,0,6] 

Note that the interspersed element only appears inside of the elements.

list elem ... item ... list elem ..... item .. list elem

Originally, I tried to write this function in terms of a fold.

intersperse' :: [a] -> a -> [a]
intersperse' ys i = foldl (\acc x -> acc ++ [x] ++ [i]) [] ys

But, my approach failed since interspersed item was getting appended to the last element. Also, the lambda appended (++) to the acc each time, which isn't goodgood.

So I decided to use pattern matching:

is' :: [a] -> a -> [a]
is' [] _ = []
is' (x:xs) i
  | null xs = [x]
  | otherwise = x : (i : (is' xs i))

Could the fold approach have worked? If so, how? Also, how's my is' implementation?

Learn You a Haskell shows the intersperse function:

ghci> intersperse '.' "MONKEY"  
"M.O.N.K.E.Y"  
ghci> intersperse 0 [1,2,3,4,5,6]  
[1,0,2,0,3,0,4,0,5,0,6] 

Note that the interspersed element only appears inside of the elements.

list elem ... item ... list elem ..... item .. list elem

Originally, I tried to write this function in terms of a fold.

intersperse' :: [a] -> a -> [a]
intersperse' ys i = foldl (\acc x -> acc ++ [x] ++ [i]) [] ys

But, my approach failed since interspersed item was getting appended to the last element. Also, the lambda appended (++) to the acc each time, which isn't good.

So I decided to use pattern matching:

is' :: [a] -> a -> [a]
is' [] _ = []
is' (x:xs) i
  | null xs = [x]
  | otherwise = x : (i : (is' xs i))

Could the fold approach have worked? If so, how? Also, how's my is' implementation?

Learn You a Haskell shows the intersperse function:

ghci> intersperse '.' "MONKEY"  
"M.O.N.K.E.Y"  
ghci> intersperse 0 [1,2,3,4,5,6]  
[1,0,2,0,3,0,4,0,5,0,6] 

Note that the interspersed element only appears inside of the elements.

list elem ... item ... list elem ..... item .. list elem

Originally, I tried to write this function in terms of a fold.

intersperse' :: [a] -> a -> [a]
intersperse' ys i = foldl (\acc x -> acc ++ [x] ++ [i]) [] ys

But, my approach failed since interspersed item was getting appended to the last element. Also, the lambda appended (++) to the acc each time, which isn't good.

So I decided to use pattern matching:

is' :: [a] -> a -> [a]
is' [] _ = []
is' (x:xs) i
  | null xs = [x]
  | otherwise = x : (i : (is' xs i))

Could the fold approach have worked? If so, how? Also, how's my is' implementation?

Source Link
Kevin Meredith
  • 3.2k
  • 28
  • 51

Writing Data.List's intersperse

Learn You a Haskell shows the intersperse function:

ghci> intersperse '.' "MONKEY"  
"M.O.N.K.E.Y"  
ghci> intersperse 0 [1,2,3,4,5,6]  
[1,0,2,0,3,0,4,0,5,0,6] 

Note that the interspersed element only appears inside of the elements.

list elem ... item ... list elem ..... item .. list elem

Originally, I tried to write this function in terms of a fold.

intersperse' :: [a] -> a -> [a]
intersperse' ys i = foldl (\acc x -> acc ++ [x] ++ [i]) [] ys

But, my approach failed since interspersed item was getting appended to the last element. Also, the lambda appended (++) to the acc each time, which isn't good.

So I decided to use pattern matching:

is' :: [a] -> a -> [a]
is' [] _ = []
is' (x:xs) i
  | null xs = [x]
  | otherwise = x : (i : (is' xs i))

Could the fold approach have worked? If so, how? Also, how's my is' implementation?