Namara

Code daily. Without assist.

Today's code

-- haskell/read

main :: IO ()
main = do
  let xs = [1, 2, undefined, 4] :: [Int]
  print (take 2 xs)

What does this print? Why doesn't undefined crash it?

Answer

[1,2]. Haskell is lazy: take 2 xs only forces the first two cons cells of the list. The undefined sitting at index 2 is never touched, so it's never evaluated — and never throws.