Skip to content

Instantly share code, notes, and snippets.

@yashigani
Created February 13, 2013 11:57
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 yashigani/4944114 to your computer and use it in GitHub Desktop.
Save yashigani/4944114 to your computer and use it in GitHub Desktop.
すごいH本読書会 in 大阪 #5
data Bookmark = Bookmark { name :: String, url :: String } |
Folder { name :: String, children :: [Bookmark] } deriving (Show, Eq)
root = Folder "root" [(Bookmark "sample1" "http://example.com"),
(Bookmark "sample2" "http://example.com"),
(Bookmark "sample3" "http://example.com")]
emptyChildren = Folder "root" []
recursibleFolder = Folder "root" [(Bookmark "sample1" "http://example.com"),
root]
toJson :: Bookmark -> String
toJson (Bookmark name url) = "{\"name\":\"" ++ name ++ "\",\"URL\":\"" ++ url ++ "\"}"
toJson (Folder name children) = "{\"name\":\"" ++ name ++ "\",\"children\":[" ++ toString children ++ "]}"
where toString xs = trancate $ foldr (\b acc -> toJson b ++ "," ++ acc) "" xs
trancate xs
| xs == [] = ""
| otherwise = init xs
addBookmark :: Bookmark -> Bookmark -> Bookmark
addBookmark (Folder n c) b = Folder n (b:c)
addBookmark b _ = b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment