-- Control structures as higher-order functions: -- while loop while :: (a -> Bool) -> (a -> a) -> a -> a while p f x | p x = while p f (f x) | otherwise = x -------------------------------------------------------------- -- Data structures implemented by higher-order functions: -- Type of arrays: type Array a = Int -> a -- The empty array does not contain an element. emptyArray :: Array a emptyArray = \i -> error "No element with this index" -- Add or set an element at some index position putIndex :: Array a -> Int -> a -> Array a putIndex a i x = a' where a' j | i==j = x | otherwise = a j -- Get an element under an index. getIndex :: Array a -> Int -> a getIndex a i = a i example1 = getIndex (putIndex (putIndex emptyArray 2 "World") 1 "Hello") 1