Gap function that returns the integer distance between first appearance of two elements in a list using either foldl or foldr.(Haskell)

不羁的心 提交于 2019-12-11 13:17:14

问题


the type is defined as follows: gap :: (Eq a) => a -> a -> [a] -> Maybe Int I have been stuck on this problem for more than an hour and have no idea how to approach the problem. I am aware that it requires the use of fold and am familiar with that topic. Please take into consideration that either foldl or foldr must be used. The output when called ought to look like this

gap 3 8 [1..10] =Just 5

gap 8 3 [1..10] =Nothing

gap 'h' 'l' "hello" =Just 2

gap 'h' 'z' "hello" =Nothing


回答1:


You might dropWhile the list until you find the starting element and then fold from the right, starting with Nothing, replacing that with Just 1 once you hit the end element, and fmaping +1 to the accumulator. In code:

gap :: Eq a => a -> a -> [a] -> Maybe Int
gap from to xs = case dropWhile (/= from) xs of
                      [] -> Nothing
                      (_:rest) -> gap' to rest

gap' :: Eq a => a -> [a] -> Maybe Int
gap' to = foldr f Nothing
    where f x acc | x == to = Just 1
                  | otherwise = (+1) <$> acc

The nice thing is that it works correctly if you have several occurences of the elements in your sequence:

*Main> gap 3 8 $ [1..10] ++ [1..10]
Just 5
*Main> gap 3 8 [1, 2, 3, 3, 3, 8]
Just 3



回答2:


Maybe my solution isn't nice, but it works

import Control.Monad
import Data.Function
import Data.Foldable     

(...) = (.) . (.)
gap x y = liftA2 ((guard . (> 0) =<<) ... liftA2 (subtract `on` fst)) 
                 (find ((==x) . snd)) (find((==y) . snd)) . zip [0..]


来源:https://stackoverflow.com/questions/49246572/gap-function-that-returns-the-integer-distance-between-first-appearance-of-two-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!