20 Replies - 4144 Views - Last Post: 31 January 2013 - 09:41 PM

Re: splitting up a string

Posted 31 January 2013 - 08:33 AM

Okay, so I think I have the labels definition correct, I am not getting errors. However, a new issue creeps up, When I call DC nothing seems to be happening. Im sorry to keep having issues, but LISP just doesnt like me haha. Once I get this done im sure ill have a much better understanding of it. But can you take a look at this code, and possibly tell me why nothing is happening?

Re: splitting up a string

Posted 31 January 2013 - 09:57 AM

You changed your toList function to return a list of strings rather than characters. That's fine, but the problem with strings is that they can't be compared using the eql function while characters can. (Basically eql acts like == does in Java). To compare strings with each other, you need to use the equalp function.

By default the member function uses eql to compare the elements of the list. So in your case, your if condition using member will always be false because member uses eql and eql always counts your strings as unequal because they aren't references to the same string in memory.

You can fix this by adding the keyword argument :test #'equalp to the call to member (which tells member to use the equalp function instead of eql - equalp works fine with strings) or by changing your toList function back to produce a list of chars rather than strings.

Re: splitting up a string

Posted 31 January 2013 - 12:50 PM

Thats strange, I don't recall changing anything. But you're right, it did return a list of strings. So I changed the code this time to make sure it gave me a list of characters. So if I run the code, just to make sure that "toList" is in fact giving me characters, I can do the following:

I don't know why its doing this, I think its related to my dC function at this point. Oddly enough, when I made this code within "defun" functions, I was able to get results that I expected, but when I convert to local functions its not the same. Im going to keep looking, but if you notice anything about my dC function that would cause this bug, please let me know

Re: splitting up a string

Posted 31 January 2013 - 09:00 PM

One thing I noticed: In toList your null str can never be true (because null works on lists, not strings and simply never returns true for strings) and is also completely unnecessary. If the string is empty the condition (= number (length str)) will be true anyway, so you don't need the other condition.

There's also no point in checking whether delim is empty in the dC method. The only way delim could be nil is if the user passed in an empty string as the delimiter to the split function. If that happens, I'd argue the correct result would be to return a list containing the whole string, not nil. I.e. (split "hello" "") should return ("hello"), not NIL. Same as if you passed in a delimiter that doesn't appear in the string. Incidentally that's the behavior you get if you simply remove the check whether delim is empty.

These points have nothing to do with your problem, but I thought I'd mention them anyway.

Re: splitting up a string

Posted 31 January 2013 - 09:15 PM

sepp2k, on 01 February 2013 - 05:00 AM, said:

I.e. (split "hello" "") should return ("hello"), not NIL. Same as if you passed in a delimiter that doesn't appear in the string. Incidentally that's the behavior you get if you simply remove the check whether delim is empty.

Which is to say, it would be the behavior if the function were working correctly. And this brings us to the problem: When you reach the end of the string, you return the empty list. So the value of str (which contains everything since the last time you've found a delimiter) will simply be discarded. That's why the "o" is missing from your result. You need to change it to return a list containing str rather than nil.

Once you did that, the result will be ("he" NIL "o"). Why the NIL in the middle? Because "hello" is split into three parts: the part before the first l, the part between the two ls and the part after the last l. Since the part between the two ls is empty, you get back a str that's never been appended to, i.e. you get back the initial value you pass in for str in the recursive call where you start with a new string. And that value is NIL.

If you use "" instead of NIL in the two places where you call dC with NIL as the third argument, you'll get back ("he" "" "o") as the result. Since that's what most split functions would return for that input, that should be the correct result.

Re: splitting up a string

Posted 31 January 2013 - 09:41 PM

sepp2k, on 31 January 2013 - 09:15 PM, said:

sepp2k, on 01 February 2013 - 05:00 AM, said:

I.e. (split "hello" "") should return ("hello"), not NIL. Same as if you passed in a delimiter that doesn't appear in the string. Incidentally that's the behavior you get if you simply remove the check whether delim is empty.

Which is to say, it would be the behavior if the function were working correctly. And this brings us to the problem: When you reach the end of the string, you return the empty list. So the value of str (which contains everything since the last time you've found a delimiter) will simply be discarded. That's why the "o" is missing from your result. You need to change it to return a list containing str rather than nil.

Once you did that, the result will be ("he" NIL "o"). Why the NIL in the middle? Because "hello" is split into three parts: the part before the first l, the part between the two ls and the part after the last l. Since the part between the two ls is empty, you get back a str that's never been appended to, i.e. you get back the initial value you pass in for str in the recursive call where you start with a new string. And that value is NIL.

If you use "" instead of NIL in the two places where you call dC with NIL as the third argument, you'll get back ("he" "" "o") as the result. Since that's what most split functions would return for that input, that should be the correct result.