I am to write a function which returns a list of all prefixes of a given string.
This is where I'm at so far.
prefixess [x] [] = [x]
prefixess [] s = prefixess [s] s
prefixess [x] s = prefixess [x, (init s)] (init s)
prefixes s = prefixess [] s
It compiles, but when I try running it on a string I get this:
Couldn't match type ‘Char’ with ‘[t]’
Expected type: [[t]]
Actual type: [Char]
Relevant bindings include
it :: [t] -> [[t]] (bound at <interactive>:18:1)
In the first argument of ‘prefixess’, namely ‘"abcde"’
In the expression: prefixess "abcde"
In an equation for ‘it’: it = prefixess "abcde"
I am out of ideas. Any hints?
I don't think this code does what you think it does. You try to pattern match the list x with the pattern [x], which captures the element of a singleton list. If i fix your code like this, it works:
This gives the following result:
But you don't really need an accumulator for a function which calculates prefixes, I would write it like this:
This function is also available under the name "inits" in Data.List