I’m supposed to split a string and return the substring that occurs before a passed character, but we’re just starting Haskell, and it is like Chinese to me. I’ve been messing with it, but no luck.
Here’s what I have so far:
--spanString returns substring of string s before char c
spanString (c, [s])::(c, [s]) -> []
spanString (c, a:b) =
let (x, y) = spanString (c, b)
in
if a < c then (a:x,y)
else (x, a:y)
What am I messing up?
Err, quite a lot.
First, your type declaration is wrong. Haskell uses upper case names for types, and it doesn’t pass parameters in brackets like most languages do. We write
instead of
You probably want something like
Your definition of spanString is syntactically right, but still wrong. Think about it this way: if first character doesn’t match then you want to spanString the rest of the string and then return the result with the first character prepended. If the first character does match then you want to return “”.