#### Archived

This topic is now archived and is closed to further replies.

# Haskell - Recursive data type

This topic is 5368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello! I'm trying to make a recursive data type consisting of a char and a link to the next char, which means that it is a string. Then I am trying to make a function that searche throught the List for a specific character:

data Test = NULL |
Word Char Test

searchChar::Test->Char->IO ()
searchChar str cha = do result <- makeIO (findChar str cha)
if result == cha
then putStr [result]

findChar::Test->Char->Char
findChar (Word cha NULL) cha2
| cha == cha2 = cha
| otherwise = ' '

findChar (Word cha pNext) cha2
| cha == cha2 = cha
| otherwise = findChar pNext cha2

makeIO::a->IO (a)
makeIO val = return(val)

makeVal::a->a
makeVal val = val


But when I call for the searchChar function like this:
searchChar ('H' ('e' ('l' ('l' ('o' (NULL)))))) 'e'

I'm getting this error by the Hugs interpretor:
ERROR - Type error in application
*** Expression     : 'H' ('e' ('l' ('l' ('o' NULL))))
*** Term           : 'H'
*** Type           : Char
*** Does not match : a -> b

I know it is the findChar function it is complaining about... something about the type I have in the findChar function, but I can't seem to figure out what is so bad about it... [edited by - bilsa on October 9, 2003 1:29:38 PM]

##### Share on other sites
I don''t know Haskell, only scanned a couple of tutorials, but it looks like you need to stick a list or tuple separator in your ''list'', it''s treating the ''H'' as a function with the rest of the ''list'' as argument. I''m not sure how you should really construct types like that, maybe the Char Test forms a tuple?

searchChar (''H'',(''e'',(''l'',(''l'',(''o'',(NULL)))))) ''e''

##### Share on other sites
Nope, that is not it... in a recursive data type you define the objects like that:

(object1 object2)

but object2 = (object2 (object3))

and so on...

##### Share on other sites
Nope, that is not it... in a recursive data type you define the objects like that:

(object1 object2)

but object2 = (object2 (object3))

and so on...

##### Share on other sites
Ah right, yeah, maybe you need the constructors explictly?

searchChar (Word ''H'' (Word ''e'' (Word ''l'' (Word ''l'' (Word ''o'' (NULL)))))) ''e''

##### Share on other sites
Yes, you need the constructors.
But why are you using a custom data type? Lists are perfect for situations like this:
searchChar :: [Char]->Char->[Char]searchChar [] _    = "Not Found"searchChar (a:k) b = searchChar k bsearchChar (a:k) a = [a  ]searchChar "Hello World" e ==> "e"

[edited by - twanvl on October 9, 2003 2:39:40 PM]

##### Share on other sites
to JuNC: Thx! Thats exactly what I needed
How stupid of me ... I added the Constructor there but used the old data type in the search.

Thx again!

to twanvl:
Yes... but this was only an easier example of what i''m actually doing...

So I wouldn''t actually make a "word" from a recursive data type

• 17
• 11
• 12
• 9
• 49
• ### Forum Statistics

• Total Topics
631395
• Total Posts
2999758
×