I made my way thru the Tutorial available at Learn your Haskell and I ask myself why the Author uses a list as the second type for the implemented Zipper?
Here is the relevant Code:
type Name = String
type Data = String
data FSItem = File Name Data
| Folder Name [FSItem]
deriving (Show)
data FSCrumb = FSCrumb Name [FSItem] [FSItem]
deriving (Show)
type FSZipper = (FSItem, [FSCrumb])
-- -------------------------------------------------------------------------------
-- Some other code he uses
-- -------------------------------------------------------------------------------
fsUp :: FSZipper -> FSZipper
fsUp (item, FSCrumb name ls rs : bs) = (Folder name (ls ++ [item] ++ rs), bs)
fsTo :: Name -> FSZipper -> FSZipper
fsTo name (Folder folderName items, bs) =
let (ls, item:rs) = break (nameIs name) items
in (item, FSCrumb folderName ls rs:bs)
nameIs :: Name -> FSItem -> Bool
nameIs name (Folder folderName _) = name == folderName
nameIs name (File fileName _) = name == fileName
x -: f = f x
-- -------------------------------------------------------------------------------
-- Example to work on
-- -------------------------------------------------------------------------------
myDisk :: FSItem
myDisk =
Folder "root"
[ File "goat_yelling_like_man.wmv" "baaaaaa"
, File "pope_time.avi" "god bless"
, Folder "pics"
[ File "ape_throwing_up.jpg" "bleargh"
, File "watermelon_smash.gif" "smash!!"
, File "skull_man(scary).bmp" "Yikes!"
]
, File "dijon_poupon.doc" "best mustard"
, Folder "programs"
[ File "fartwizard.exe" "10gotofart"
, File "owl_bandit.dmg" "mov eax, h00t"
, File "not_a_virus.exe" "really not a virus"
, Folder "source code"
[ File "best_hs_prog.hs" "main = print (fix error)"
, File "random.hs" "main = print 4"
]
]
]
The commands you can use:
*Filesystem> let newFocus1 = (myDisk,[]) -: fsTo "programs" -: fsTo "source code"
*Filesystem> let newFocus2 = (myDisk,[]) -: fsTo "pics" -: fsTo "ape_throwing_up.jpg"
No matter what I do I always end up with a list with only one Item, so wouldn't it be better to use just FSCrumb
instead of [FSCrumb]
?
The problem was simply the method used to obtain the number of breadcrumbs; a simple (and correct) way is:
numberOfBreadcrumbs :: FSZipper -> Int
numberOfBreadcrumbs (_, breadcrumbs) = length breadcrumbs