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.
seriesadds all the elements in the given list.parallel1) takes the inverse of each number in the list, then 2) adds all the elements in the list, then 3) takes the inverse of the sum of the elements.
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.
'notation in Lisp is very different from quoted character strings. There is no closing quote.'<anything>is a shorthand which stands for the list structure(quote <anything>). This is useful because when(quote <anything>)is evaluated, the value is always object<anything>itself, whatever it is. The value of(quote (+ 2 2))is the list(+ 2 2)rather than4. Lisp has a double quote for string literals, like"abc", and character constants have a '#\' (hash backslash) syntax which has some variety in it such as '#\space' denoting space and '#\A' denoting upper case A.mapcarstops when shorter list ends. This means, circular list can be used to supply the default argument:(nth 5 (setq b '(1.0) b (rplacd b b))) ==> 1.0. Don't know about emacs-Lisp though. btw do you get same answer when using1and1.0?mapcarare guaranteed to be the same length. I also saw'#1='(1 2 3 . #1#)as an example of a circular list. Does this work?