repl.it
@anonymous/

CuteJealousNorwaylobster

Haskell

No description

fork
loading
Files
  • main.hs
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
nearestNonzero :: [Int] -> Int -> Int
nearestNonzero xs n
    | pivot > 0 = pivot
    | otherwise = nearestNonzero' (reverse before) after
    where pivot = xs !! n
          (before, after) = (take n xs, drop (n+1) xs)

nearestNonzero' :: [Int] -> [Int] -> Int
nearestNonzero' (b:bs) (a:as)
    | b > 0 = b
    | a > 0 = a
    | otherwise = nearestNonzero' bs as
nearestNonzero' bs as
    | null rest = 0
    | otherwise = head rest
    where nonemptyList = if null bs then as else bs
          rest = dropWhile (<=0) nonemptyList
GHCi, version 8.6.5