Hello LispForum,I'm new here as you can see from this being my first post. I'm a college student and am currently taking a computational psychology course. We started covering the basics of Lisp and I felt pretty comfortable and knew my way around. However, I'm running into some difficulty here and there. I'm not sure if this post will be considered okay, but I'll give it a try.

Here are some of the functions I have to define as well as where I'm at. I might be going the wrong way.. but hopefully you guys can nudge me to the right direction.

1. Use mapcar and lambda to define (add-to-list x lst) which adds the number x to each of a list of numbers and returns the list of results in the same order.

I have moved your post to the Common Lisp subforum. I assume that the language used is CL. Do not that Lisp is a family of fairly diverse languages, and you should know and specify which specific language is the one used. Also use code tags for including code.

Tetrix wrote:I think this is the right direction.. I'm just unaware if there is a function call that gives you a list filled with one variable (illustrated here by '(x x x x x)..).

You don't need the list of the number added. The purpose of this exercise is to demonstrate that an anonymous function created by LAMBDA can access variables of the enclosing scope, which means you can have an anonymous function of one argument, which will be the elements of the list you are adding to, and the number being added you can refer to just by referencing the variable in the named function.

Tetrix wrote:2.Use do, if, oddp, and return to define (first-odd lst) which returns the first odd integer in a list of integers or ‘none if none are odd.

I am not entirely sure here, because in practice DO is very rarely used directly, since other forms of iterations are easier to understand. And so is direct use of RETURN. Code created within those constraints is necessarily unidiomatic.

The way I see to approach it is to use IF in the end condition. You should first check if the list still has elements, then in the second IF check if the first number is ODDP, if it is, then RETURN. Also note that DO has a result-form, which will be returned if the iteration finished properly, which is one place to return NONE from.

Tetrix wrote:And if this is an inappropriate post, I'm very sorry.

It is quite appropriate to ask for help with homework and similar as long as you show attempt to understand it yourself, which you did. Hopefully I have helped, and if not, please ask further questions.

(defun add-to-list (x lst) "(x lst)Adds the number x to each of a list of numbers and returns the list of results in the same order" (mapcar (lambda (a b) (+ a b)) '(x x x x x x x) '(lst)))

I think this is the right direction.. I'm just unaware if there is a function call that gives you a list filled with one variable (illustrated here by '(x x x x x)..).

'(x x x x x x x) is a list of 7 copies of the symbol named "X", not seven copies of the value of the function parameter. This will cause a runtime error when you try to add symbols. You could use (list x x x x x x x), but of course you don't know how many copies you need. If you want to do this sort of thing, write

Thanks so much for the help guys! I've been super busy with other courses lately. But I'll be sure to digest all the feedback and try making this work! I'll post back in 3 days with what I can figure out by then.Quick edit: Number 2 works out now! I guess I didn't grasp the way mapcar mapped functions properly, but now it appears more intuitive then it did at first!Final solution:(defun add-to-list (x lst) "(x lst)Adds the number x to each of a list of numbers and returns the list of results in the same order" (mapcar (lambda (y) (+ x y)) lst))

It actually doesn't, it gives you the final number if it is odd and NIL otherwise, because the implicit false clause of the IF is overwriting your odd number storage variable. Not to mention so does the true branch, which means it would return the last odd number anyway. The point of using RETURN in this exercise is to break the DO iteration before the list runs out. Since you have to return only the first odd number, there is no point in iterating over all the remaining numbers.

RETURN is not actually a function, but a macro over a special operator RETURN-FROM. That special operator can be used to return from a named block of code. RETURN is a short form which returns from a block named NIL, which is established implicitly by many forms, including DO.

In short, RETURN does what is says: it immediately returns its argument from a computation. Using it explicitly is fairly rare in idiomatic Lisp code. Normally you would just check if the current number is odd in the termination test, but since the exercise includes RETURN explicitly I guess you are supposed to use it to return the odd number once the iteration finds one.

I figured it out!(defun first-odd (lst)"(lst)Returns the first odd integer in a list of integers or ‘none if none are odd."(do ((numbers lst (cdr numbers)))((null numbers) 'none)(if (oddp (car numbers))(return (car numbers)))))Thanks for the help, I have 2 more problems to do for this assignment and one of them simply .. confuses me. I'll give you a preview of the numbers before I start trying to figure them out over the next 3 days:

4. Use cond and exp to define (activation type sum) which returns the activation valueof a connectionist unit given a sum of products of input activations x correspondingconnection weights. Include both ‘sigmoid and ‘asigmoid types of activationfunctions, and return ‘unknown-type if some other type is used as input.

5. Use do, if, and funcall to define (satisfy proc lst) which returns a list of the items in alist that satisfy a procedure. An item satisfies a procedure if the procedure returns truewhen that item is used as the procedure's argument.