Skip to content

Instantly share code, notes, and snippets.

@realgenekim
Created September 4, 2018 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realgenekim/47150f7acd8b8700a499ef51c3bafff7 to your computer and use it in GitHub Desktop.
Save realgenekim/47150f7acd8b8700a499ef51c3bafff7 to your computer and use it in GitHub Desktop.
-- Build this with "ghc ibid.hs"
-- Test it with "./ibid"
-- Import some list functions
import Data.List
-- Some refs to test on
refs = ["book1","book2","ibid","ibid","paper1","ibid","paper2","book3"]
-- Code close to what John wrote that will work in ghci
addRef curr_ref (last_ref,refList) =
if curr_ref == "ibid" then
(last_ref, last_ref:refList)
else
(curr_ref, curr_ref:refList)
flushIBID refs = reverse (snd(foldr addRef ("unknown",[]) (reverse refs)))
-- Function to try out an implementation
tryIt name func input = do
putStrLn $ "Trying out function " ++ name
putStrLn $ " input = " ++ (show input)
putStrLn $ " output = " ++ (show (func input)) ++ "\n"
-- Try it out
tryIBID = tryIt "flushIBID" flushIBID refs
-- Alternate implementation (also ghci compatible)
expandCurrRef prev_ref cur_ref = if cur_ref == "ibid" then prev_ref else cur_ref
flushIBID2 refs = scanl1 expandCurrRef refs
-- Try it out
tryIBID2 = tryIt "flushIBID2" flushIBID2 refs
-- Inverse transformation (add IBIDs)
ibidCurrRef prev_ref cur_ref = if prev_ref == cur_ref then (prev_ref,"ibid") else (cur_ref,cur_ref)
addIBID refs = snd (mapAccumL ibidCurrRef "unknown" refs)
-- Try it out
tryAddIBID = tryIt "addIBID" addIBID (flushIBID refs)
main = do
tryIBID
tryIBID2
tryAddIBID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment