Level of Haskell : Newbie
Goal : Find the root of an element of the tree represented as a List
Input (Tree Nodes) (Positions in Array denote the node number) : [0,1,9,4,9,6,6,7,8,9]
Function invoked : getRoot 3
Expected Output : 9
Code :
li = [0,1,9,4,9,6,6,7,8,9]
getRoot::Integer->Integer
getRoot n | li!!n /= n = getRoot li!!n
getRoot n | otherwise = li!!n
Error Message :
ERROR file:.\test2.hs:111 - Type error in application *** Expression : li !! n *** Term : n *** Type : Integer *** Does not match : Int
Compiler : WinHugs
Tried various combinations of 'Integers' and 'Int' to declare the type of the function. It seems that the array access returns an Integer but is then compared to an Int where it fails. Do not know why it does not convert Int to Integers.
Or is it something else all together ?
Searched on the internet, in the tutorials and on stackoverflow.
(!!)
or any other index-based function on lists, includinghead
, under any circumstances. If you think you need to use those functions, change your algorithm or use a different data structure instead. Continue to avoid these functions until you've learned Haskell well enough to know why I'm giving this advice. – C. A. McCann(!!)
and using that does not count as not using(!!)
. =) – Daniel Wagner