From a697cfd93204e408fb9ac8363e3379f5a12e172e Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Sun, 30 Nov 2014 12:00:18 +1100 Subject: IP functions now more tolerant of error conditions --- src/Grasp/IP.hs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Grasp/IP.hs b/src/Grasp/IP.hs index efb904d..6a4d452 100644 --- a/src/Grasp/IP.hs +++ b/src/Grasp/IP.hs @@ -2,10 +2,11 @@ module Grasp.IP ( IP, singleton, + empty, + isEmpty, peek, push, - pop, - isEmpty + pop ) where @@ -25,15 +26,18 @@ newtype IP = IP [GNode] singleton :: GNode -> IP singleton n = IP [n] -peek :: IP -> GNode -peek (IP p) = head p +empty :: IP +empty = IP [] + +isEmpty :: IP -> Bool +isEmpty (IP p) = (length p == 0) + +peek :: IP -> Maybe GNode +peek (IP p) = if (length p == 0) then Nothing else Just (head p) push :: GNode -> IP -> IP push n (IP p) = IP (n:p) pop :: IP -> IP -pop (IP p) = IP (tail p) - -isEmpty :: IP -> Bool -isEmpty (IP p) = (length p == 0) +pop (IP p) = if (length p == 0) then IP p else IP (tail p) -- cgit