Find first missing positive: Difference between revisions

→‎{{header|Haskell}}: Added a definition in terms of standard library functions
(Add BQN)
(→‎{{header|Haskell}}: Added a definition in terms of standard library functions)
Line 268:
{{out}}
<pre>[3,2,1]</pre>
 
 
Or, expressed in terms of an infinite list, and standard library functions:
<lang haskell>import Data.List (find, notElem)
import Data.Maybe (fromJust)
 
---------- FIRST MISSING POSITIVE NATURAL NUMBER ---------
 
firstGap :: [Int] -> Int
firstGap xs = fromJust $ find (`notElem` xs) [1 ..]
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
fmap
(\xs -> show xs <> " -> " <> (show . firstGap) xs)
[ [1, 2, 0],
[3, 4, -1, 1],
[7, 8, 9, 11, 12]
]</lang>
{{Out}}
<pre>[1,2,0] -> 3
[3,4,-1,1] -> 2
[7,8,9,11,12] -> 1</pre>
 
=={{header|jq}}==
{{works with|jq}}
9,655

edits