Scanning database...
Tools
Articles

No matches found for ""

View All Results
Home Dev Lab haskell
Developer Lab

UUID in Haskell

Production-ready implementation guide with CSPRNG-backed code snippets.



Generate UUID in Haskell

UUID generation in Haskell using the uuid package from Hackage — pure, type-safe, CSPRNG-backed.

Implementation Code

Production Ready
haskell snippet
import Data.UUID.V4 (nextRandom)
import Data.UUID    (toString)

main :: IO ()
main = do
    -- Generate a single UUID v4
    uuid <- nextRandom
    putStrLn (toString uuid)

    -- Generate multiple UUIDs
    uuids <- mapM (\_ -> nextRandom) [1..5 :: Int]
    mapM_ (putStrLn . toString) uuids

Explanation

The uuid package on Hackage provides a strongly-typed UUID type. Data.UUID.V4.nextRandom generates a version 4 UUID in the IO monad using the system's CSPRNG. toString converts it to the standard hyphenated string format.

Output Example

b4e1f2a3-7c8d-4e9f-a0b1-c2d3e4f5a6b7

Best Practice

Use the UUID type directly in your data types rather than converting to String early. This keeps type safety throughout your application. The uuid-types package provides a lightweight alternative if you only need the type.

Performance

Very good. The uuid package is efficient and the GHC runtime handles IO operations well. Suitable for high-throughput Haskell services.

Installation

Add to your package.yaml or .cabal file:

dependencies: - uuid

Then: cabal install uuid or stack build

Security

Cryptographically secure. nextRandom uses the system's CSPRNG (/dev/urandom on Linux, CryptGenRandom on Windows).