Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/compiler98/Memo.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


module Memo(Memo,initM,listM,fromListM,elemM,lookupM,addM) where

import Tree234

newtype Memo a = Memo (Tree a)

initM :: Memo a
initM = Memo initTree

listM :: Memo a -> [a]
listM (Memo m) = treeMapList (:) m

fromListM :: Ord a => [a] -> Memo a
fromListM es = foldl addM initM es

elemM :: Ord a => Memo a -> a -> Bool
elemM (Memo tree) value = 
  treeSearch False (\_ -> True) (compare value) tree

lookupM :: Ord a => Memo a -> a -> Maybe a
lookupM (Memo tree) value =
  treeSearch Nothing Just (compare value) tree

addM :: Ord a => Memo a -> a -> Memo a
addM (Memo tree) value =
  Memo $ treeAdd comb compare value tree
 where
  comb a b = b

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.