Zchpyvr
Zchpyvr

Reputation: 1129

LISP- simple functions , syntax error?

I'm currently trying to learn lisp and am using emacs on linux. To the best of my ability, I have written two functions.

Both functions first remove the first element of the list.


Code:

(defun series (lst)
    (apply #'+' (cdr lst)) )

(defun parallel (lst)
    (/ 1 (apply #'+' (apply #'/' (cdr 'lst ) ) ) ))

I can evaluate the function, but when I try to use the function, as below:

(series (list 3 3 4 5))

I get the error : value CDR is not of the expected type NUMBER. I see this, and I think, why is emacs treating cdr as a number rather than a function? I'm new to lisp and emacs, so I don't know to fix this error. Any help would be appreciated.


UPDATE

I've the problems in this code and I think it works...

(defun series (lst)
 (apply #'+ (cdr lst) ))

(defun parallel(lst)
(/ 1 (apply #'+ (mapcar #'/ (make-list (- (length lst) 1) :initial-element 1) (cdr lst)  ) )))

Hopefully, what I was trying to do is understood now.

Upvotes: 2

Views: 576

Answers (2)

Kaz
Kaz

Reputation: 58578

Re:

(defun parallel (lst)
    (/ 1 (apply #'+' (apply #'/' (cdr 'lst ) ) ) ))

Why are you trying to discard the first element. That is curious.

Note that the logic of this function won't work even if the quoting issues are solved.

(apply #'/ (cdr lst)) will pass the remainder of lst to the / function. This function will produce a number.

And so you then effectively (apply #'+ number), problem being that apply wants a list.

The parallel semantics you are hinting at will not happen.

Perhaps you want this:

 ;; divide the sum of the remaining elements, by the fraction of those elements
 (/ (apply #'+ (cdr lst)) (apply #'/ (cdr lst)))

Note that in Emacs Lisp, division of integers by integers yields an integer, and not a rational like in Common Lisp.

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95252

You have extra apostrophes that are confusing the LISP parser. The syntax to refer to the + function is just #'+; there's no "close quote".

Upvotes: 3

Related Questions