refactored P27.hs to align with F# version