index too large

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

index too large

Nathan M. Holden
I've been working on a quick program that acts (vaguely) like a console, lets
me type notes and outputs them in a .tex file. I figure it's not that
complicated, but it uses a vaguely n-ary structure, so that I can have headers
and sub-notes, which loos like:

data Note = N {
  nTitle  :: {Char},
  nBody :: {Char},
  nFormat :: Format,
  subs :: [Note]
| E [Char]

Format's just a collection of preset formats, so I don't have to dynamically
handle that (since I didn't need to for my uses).

Anyways, for whatever reason, sometimes while adding notes, it sends me an
error: Prelude.(!!): index too large

My only 2 uses of that are in
getFullName :: [Note] -> [Int] -> [Char]
getFullName d [] = "/"
getFullName d (x:xs) = "/" ++ getPartName (d!!x) ++ getFullName (subs (d!!x))
xs

and

moveIn :: [Note] -> [Int] -> [Char] -> Int -> [Int]
moveIn [] i m c = reverse i
moveIn ((N dt db df du):ds) [] m c = if matchNote (N dt db df du) m then c:[]
else moveIn ds [] m (c+1)
moveIn n i m c = last i : ((moveIn (subs (n!!(last i)))) (allButLast i) m 0)

where the [Int] is the variable I use to track where in the tree I am. Since
I'm not moving into a Note that doesn't exist, and the display shouldn't be
changing, what could be the problem?

This is probably too in-depth a question, now that I think about it...
Reply | Threaded
Open this post in threaded view
|

index too large

Daniel Fischer-4
Am Freitag 13 November 2009 03:55:40 schrieb Nathan M. Holden:

> I've been working on a quick program that acts (vaguely) like a console,
> lets me type notes and outputs them in a .tex file. I figure it's not that
> complicated, but it uses a vaguely n-ary structure, so that I can have
> headers and sub-notes, which loos like:
>
> data Note = N {
>   nTitle  :: {Char},
>   nBody :: {Char},
>   nFormat :: Format,
>   subs :: [Note]
>
> | E [Char]
>
> Format's just a collection of preset formats, so I don't have to
> dynamically handle that (since I didn't need to for my uses).
>
> Anyways, for whatever reason, sometimes while adding notes, it sends me an
> error: Prelude.(!!): index too large

Obviously, sometimes you call list!!index with index > (length list - 1).
Are you aware that the indices are 0-based?
If it's not that, one would have to analyse the code, but it would be better to see more
of it for that.