Being that I'm having trouble with practically every aspect of this function, I figure I can start from the top and hopefully by the end I will have everything figured out. First, Ill start with trying to iterate through a string, converting each character into a list. I actually have something thats somewhat close here, but youll see why its not right.

Re: splitting up a string

I believe the #\ is just Lisps way of saying "this is a char". Like, if it was just (h e l l o), how would you know if h was a char or an atom?

Yup I figured that out lol. I also figured out how to turn something into a local function, but the website wasnt loading properly for me to update. However, I still have other issues with this function that need to be addressed. So, if someone could help me with the following:
EDIT: I got this part look at the next post

the idea behind the code after this is that it will take a string, and a string of delimiters and a number (starting point, usually 0)
it will check to see if str matches any character in delim, if it does, return true.

toList converts a string to a list of characters
dCheck checks each character to see if its a delimiter

Im attempting a for loop to go through and rebuild a string from a list, but can you even do that? Ive only ever done it with recursion, and recursion doesnt seem like it makes sense in this context, you dont want to constantly redefine local functions and redo all the other work being doing in this

Re: splitting up a string

Posted 29 January 2013 - 07:47 PM

First of all, you should really indent your code properly and don't write too much on one line - the then- and else-parts of an if should be each on its own line. This makes errors like "too many arguments for if" much easier to detect.

;loop through the list from STR, checking if there are delimiters
(loop for i in (toList str 0) do
(dCheck (string i) delim 0)
);end for

That won't work. Your code above is equivalent to this in an imperative language:

for(i in toList(str, 0)) {
dCheck(string(i), delim, 0);
}

where dCheck is a boolean function with no side-effects. Consequently it will simply not do anything. Generally you should only use loop ... do with a body that causes side-effects (like if you're printing the values inside the body).

Quote

Im attempting a for loop to go through and rebuild a string from a list, but can you even do that?

You shouldn't be using loop ... do. The only way to produce values using loop ... do would be by using side-effecting functions like setf/setq and that's not very lispy. If you use loop, you should be using loop ... collecting or loop ... appending or something like that. However if you haven't covered loop yet, you probably shouldn't be using loop at all.

Quote

Ive only ever done it with recursion, and recursion doesnt seem like it makes sense in this context, you dont want to constantly redefine local functions and redo all the other work being doing in this

Right, that's why, in cases like this, it often makes sense to define a local function that's recursive. So if the local function recurses, the definitions of the outer function don't get redefined.

Re: splitting up a string

Posted 29 January 2013 - 07:56 PM

sepp2k, on 29 January 2013 - 07:47 PM, said:

First of all, you should really indent your code properly and don't write too much on one line - the then- and else-parts of an if should be each on its own line. This makes errors like "too many arguments for if" much easier to detect.

;loop through the list from STR, checking if there are delimiters
(loop for i in (toList str 0) do
(dCheck (string i) delim 0)
);end for

That won't work. Your code above is equivalent to this in an imperative language:

for(i in toList(str, 0)) {
dCheck(string(i), delim, 0);
}

where dCheck is a boolean function with no side-effects. Consequently it will simply not do anything. Generally you should only use loop ... do with a body that causes side-effects (like if you're printing the values inside the body).

Quote

Im attempting a for loop to go through and rebuild a string from a list, but can you even do that?

You shouldn't be using loop ... do. The only way to produce values using loop ... do would be by using side-effecting functions like setf/setq and that's not very lispy. If you use loop, you should be using loop ... collecting or loop ... appending or something like that. However if you haven't covered loop yet, you probably shouldn't be using loop at all.

Quote

Ive only ever done it with recursion, and recursion doesnt seem like it makes sense in this context, you dont want to constantly redefine local functions and redo all the other work being doing in this

Right, that's why, in cases like this, it often makes sense to define a local function that's recursive. So if the local function recurses, the definitions of the outer function don't get redefined.

Thats definitely something I noticed... that the way I was going about this wasn't very "lispy", but unfortunately, my experience with Lisp is very limited (which also explains why my indentation is so bad, I will look at that link)

So, overall, what do you think my strategy should actually be to complete this function? Should I write additional local functions, perhaps one to replace the loop(but recursive)? I ask, because it seems very easy to get away from the way Lisp is meant to work if you're not used to functional programming.

First of all you might be misunderstanding how append works. Bboth arguments to append should be lists. And even more importantly: (append list1 list2) returns a list that contains both the elements of list1 and list2. It does not change the contents of list1. So if you call append and don't do anything with its result, you're doing it wrong.

That's because you wrote ( (append ...) (setq ...) ). In Lisp parentheses surround function or macro calls. So in the above Lisp thinks that (append ...) is the function you're trying to call and (setq ...) is the argument you're trying to call it with. That is it thinks that (append ...) will return a lambda (anonymous function).

If you want to evaluate multiple side-effecting functions after each other, you should use (progn (append ...) (setq ...)), but as I already said, append isn't a side-effecting function, so that won't do what you want either (it will get rid of the error though). Generally you should avoid progn.

Re: splitting up a string

Posted 29 January 2013 - 08:16 PM

NecroWinter, on 30 January 2013 - 03:56 AM, said:

So, overall, what do you think my strategy should actually be to complete this function? Should I write additional local functions, perhaps one to replace the loop(but recursive)? I ask, because it seems very easy to get away from the way Lisp is meant to work if you're not used to functional programming.

I think the easiest solution here would be a recursive function. There's nothing unlispy about using loop¹ (as long as you don't use loop ... do + set functions), but to do what you want here, the loop would probably get a bit complicated and you don't seem to be all that familiar with the loop construct yet.

¹ As far as I am concerned, anyway. Some people argue that loop is unlispy by nature because it has a lot of English keywords and not a lot of parens (or something like that).

Re: splitting up a string

Posted 30 January 2013 - 05:09 PM

Ok, so luckily my professor pushed back the due date, and I'm actually making a lot of progress! One small issue though, I have a function that takes two lists of characters (one is from a string that contains a phrase or a word, the other is a list of delimiters)

Im also trying to make my code more readable, but I have not had time to read the link sepp gave me on formatting code.

So take a look at my code at the bottom, the function dC does most of the work, the function toList seems like its correct, and its formatted poorly, so feel free to not even look at it.

Re: splitting up a string

In my code I replaced the nil return instruction with "" but it causes really strange errors, and its very clearly not correct.

What exactly do you mean by the "nil return instruction"? In your code there are two places where you call dC with nil as the third argument. I think if you replace those nils with "", it will work. If that's what you meant that causes the strange errors, please elaborate what those errors are.

Re: splitting up a string

In my code I replaced the nil return instruction with "" but it causes really strange errors, and its very clearly not correct.

What exactly do you mean by the "nil return instruction"? In your code there are two places where you call dC with nil as the third argument. I think if you replace those nils with "", it will work. If that's what you meant that causes the strange errors, please elaborate what those errors are.

The strange errors that I spoke of came from the fact that I had "dc" previously defined in my clisp session, it was running old code. Turns out, the code I have presented has a syntax error and DC is not getting defined correctly anyway. I am trying to figure out what I did wrong but I would appreciate someone stepping in and pointing out where my syntax is wrong

by nil return instruction I meant the very first nil you see in DC, but I want to get the syntax correct for labels first

Re: splitting up a string

Posted 30 January 2013 - 11:39 PM

So basically whats happening is that im creating a local function and then not using it because its closed off. The only reason I thought my code worked is because I am in a clisp session testing code, and my functions were previously defined. If I can get them locally declared correctly, im sure the code will work.

So heres a straight forward question, what is the syntax to define two local functions? I cant find this online anywhere, but im sure you can do it. Every example I find online is meant for one local function