Is there a way I can apply '+ to '( 1 2 3)?
edit: what i am trying to say is that the function i get will be a symbol. Is there a way to apply that?
Thanks.
(apply (eval '+) '(1 2 3))
Should do it.
In R5RS you need
(apply (eval '+ (scheme-report-environment 5)) '(1 2 3))
The "Pretty Big" language in Dr. Scheme allows for:
(apply (eval '+) '(1 2 3))
How about 'apply'? Use the variable + instead of the symbol + .
(apply + '(1 2 3))
R5RS
;; This works the same as funcall in Common Lisp:
(define (funcall fun . args)
(apply fun args))
(funcall + 1 2 3 4) => 10
(funcall (lambda (a b) (+ a b) 2 3) => 5
(funcall newline) => *prints newline*
(apply newline) => *ERROR*
(apply newline '()) => *prints newline*
Btw, what's the deal with this "syntax highlighting" ??
In Racket's scheme it would be
#lang scheme
(define ns (make-base-namespace))
(apply (eval '+ ns) '(1 2 3))
How about the scheme "apply"
(apply + `(1 2 3)) => 6
I hope that was what you were asking :)
Related
I set myself to the task to write a Common Lisp function that concatenates two lists without using append.
Common Lisp input (concat-lists '(1 2 3) '(4 5 6)) should return (1 2 3 4 5 6)
Even though my solution seems to work it looks overtly complicated
(defun concat-lists(seq1 seq2)
(cond ((not (null seq1)) (cons (car seq1) (concat-lists (cdr seq1) seq2)))
(T (cond ((not (null seq2)) (cons (car seq2) (concat-lists seq1 (cdr seq2))))
(T nil)))))
What I'm looking for is a more elegant solution using reduce where I use seq1 as initial value and then apply a function to each element of seq2, thereby appending each value of the list to seq1. Somehow I always get stuck when trying....
Any help or input is much appreciated. Thanks!
CL-USER 39 > (reduce #'cons
'(1 2 3 4 5)
:initial-value '(a b c d e)
:from-end t)
(1 2 3 4 5 A B C D E)
The solution of Rainer Joswig is really elegant and simple, and respects your request of using reduce.
If you want to see also a recursive, simple solution, then here is the classical one:
(defun concat-lists (seq1 seq2)
(if (null seq1)
seq2
(cons (car seq1) (concat-lists (cdr seq1) seq2))))
(concat-lists '(1 2 3) '(4 5 6))
(1 2 3 4 5 6)
I do understand your requirement for 'reduce'. and here other options:
CL also has 'concatenante'
(concatenate 'list '(1 2 3) '(4 5 6))
There is also the other less complicated (IMHO), and not as elegant.
(defun concat-lists (list1 list2)
(let ((a (copy-list list1))
(b (copy-list list2)))
(rplacd (last a) b)
a))
or
(defun concat-lists (list1 list2)
(let ((a (copy-list list1))
(b (copy-list list2)))
(nconc a b)))
In Common Lisp, I can get a function to pass around with the #' syntax, like this:
(let ((x #'+))
(funcall x 1 2))
But suppose I want to set a function so I don't have to use funcall for it. Does Common Lisp have a local function name table, or just the global one that is assigned to with defun?
Is there a way to assign to a function symbol other than defun? Or more generally: is there a way I can do something similar to this nonworking example:
(setf #'x #'+)
(x 1 2)
You can define a local function using
flet and labels:
(flet ((f (x) (1+ (* 2 x))))
(f 7))
==> 15
You can also set function definition of a symbol using fdefinition:
(setf (fdefinition 'f) #'+)
(f 1 2 3)
==> 6
Note that let binds the
value cell of the symbol while flet bind the function cell.
When the symbol appears in the "function" position, the "function"
cell is used, while when it appears in the "value" position, the "value"
cell is used:
(setf (symbol-function 'x) #'car)
(setf (symbol-value 'x) #'cdr)
(x '(1 . 2))
==> 1
(funcall x '(1 . 2))
==> 2
Similarly,
(flet ((x (o) (car o)))
(let ((x #'cdr))
(cons (x '(1 . 2))
(funcall x '(1 . 2)))))
==> (1 . 2)
This is the difference between Lisp-1 and Lisp-2.
Finally, note that CLISP is just one implementation of the language ANSI Common Lisp.
One option for getting this kind of behavior is to write a macro to do it.
(defmacro flet* (assignments &body body)
(let ((assignments (mapcar
(lambda (assn)
(list (first assn) '(&rest args)
(list 'apply (second assn) 'args)))
assignments)))
`(flet ,assignments ,#body)))
This macro translates flet* into flet + apply like this:
(flet* ((x #'+)
(y #'*))
(pprint (x 1 2))
(pprint (y 3 4))
(pprint (x (y 2 3) 4)))
Becomes:
(flet ((x (&rest args) (apply #'+ args))
(y (&rest args) (apply #'* args)))
(pprint (x 1 2))
(pprint (y 3 4))
(pprint (x (y 2 3) 4)))
I'm using R5RS (in racket). Here is my example
(map (lambda (x) (list (apply + x) (apply * x))) '((1 2 3) (4 5 6)))
-> ((6 6) (15 120))
I was just wondering that if there's a built-in function like this
(map-apply '(+ *) '((1 2 3) (4 5 6)))
which could produce the same result.
Nothing built-in, but you can write your own:
(define (map-apply funcs items)
(map (lambda (item)
(map (lambda (func)
(apply func item))
funcs))
items))
You'd have to pass in (list + *), not '(+ *), however.
If you are doing these kinds of things often you can abstract the production of the map-function. Thus the rest will be unchanged:
;; Makes a function that applies all functions to an argument
(define (make-map-fun . funs)
(lambda (lis)
(map (lambda (fun) (apply fun lis)) funs)))
(map (make-map-fun + *) '((1 2 3) (4 5 6)))
; ==> ((6 6) (15 120))
;; And you can zip with it too
(apply map
(lambda l ((make-map-fun + *) l))
'((1 2 3) (4 5 6) (7 8 9)))
; ==> ((12 28) (15 80) (18 162))
Are you still there +firegun? If you really, really need to pass the function arguments as '(+ *) then your solution would be:
(define (map-apply function-names items)
(let ((funcs (map (lambda (name) (eval name (interaction-environment))) function-names)))
(map (lambda (item)
(map (lambda (func)
(apply func item))
funcs))
items)))
The following Emacs Lisp function takes a list of lists and returns a list in which the items of the inner lists have been concatenated to one big list. It is pretty straight-forward and I am convinced something like this must already be part of the standard function library.
(defun flatten (LIST)
(if LIST
(append (car LIST) (flatten (cdr LIST)))
nil))
I am looking for a function that will take a single list of lists as its argument and then append all the inner lists.
(flatten '((a b) (c d)))
will give
(a b c d)
Does anyone know whether this function is already built in, and if so, under which name?
Thanks!
You're either looking for append:
(defun flatten (list-of-lists)
(apply #'append list-of-lists))
If (and only if) you know that you'll always have a list of lists.
Otherwise:
(defun flatten (list)
(mapcan (lambda (x) (if (listp x) x nil)) list))
Emacs 27.1 has flatten-tree:
(flatten-tree '((a b) (c d)))
(a b c d)
See: https://www.gnu.org/software/emacs/manual/html_node/elisp/Building-Lists.html
I stepped into this only recently whilst looking for something different; there is something that might not have been put into evidence by the test data utilized to check the function, depending on whether the original question was meant to refer to generic lists (i.e.: list of list of list of list of...) or just to two-level lists (as in the example).
The solution based on append works fine only with two-level lists, and there is a further issue with the solution based on mapcan.
Basically, the general solution has to be recursive both on car and cdr, as in the flatten defun below.
(setq l '((((1 2) 3) 4) (5 6 7)))
(defun flatten(x)
(cond ((null x) nil)
((listp x) (append (flatten (car x)) (flatten (cdr x))))
(t (list x))))
(defun flatten2(l)
(if l (append (car l) (flatten2 (cdr l))) nil))
(defun flatten3(l)
(mapcan (lambda(x) (if (listp x) x nil)) l))
(flatten l)
(1 2 3 4 5 6 7)
(apply #'append l)
(((1 2) 3) 4 5 6 7)
(flatten2 l)
(((1 2) 3) 4 5 6 7)
The further issue is with the usage of mapcan in flatten3: as mapcan hides an nconc inside, the user must remember that it alters its argument.
l
((((1 2) 3) 4) (5 6 7))
(flatten3 l)
(((1 2) 3) 4 5 6 7)
l
((((1 2) 3) 4 5 6 7) (5 6 7))
Dash is a modern list library for Emacs, and has flatten. It's the second most downloaded package on Melpa, after magit. From the readme:
-flatten (l): Takes a nested list l and returns its contents as a single, flat list.
(-flatten '((1))) ;; => '(1)
(-flatten '((1 (2 3) (((4 (5))))))) ;; => '(1 2 3 4 5)
(-flatten '(1 2 (3 . 4))) ;; => '(1 2 (3 . 4))
-flatten-n (num list): Flatten num levels of a nested list.
(-flatten-n 1 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 (3 4) ((5 6)))
(-flatten-n 2 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 3 4 (5 6))
(-flatten-n 3 '((1 2) ((3 4) ((5 6))))) ;; => '(1 2 3 4 5 6)
This package was started 2012-09.
I realize that the original question was "what is the built in function". It appears that there is none. The other solutions do not actually flatten all lists that I tested. This function appears to work. I'm posting it here because this was the first place Google hit when I did my search.
(defun flatten (LIST)
"flattens LIST"
(cond
((atom LIST) (list LIST))
((null (cdr LIST)) (flatten (car LIST)))
(t (append (flatten (car LIST)) (flatten (cdr LIST))))))
e.g.
(flatten (list "a" (list "b" "c" nil) (list (list "d" "e") "f")))
("a" "b" "c" nil "d" "e" "f")
Have a look at nconc
This works:
(+ 1 2 3)
6
This doesn't work:
(+ '(1 2 3))
This works if 'cl-*' is loaded:
(reduce '+ '(1 2 3))
6
If reduce were always available I could write:
(defun sum (L)
(reduce '+ L))
(sum '(1 2 3))
6
What is the best practice for defining functions such as sum?
(apply '+ '(1 2 3))
If you manipulate lists and write functional code in Emacs, install dash.el library. Then you could use its -sum function:
(-sum '(1 2 3 4 5)) ; => 15
Linearly recursive function (sum L)
;;
;; sum
;;
(defun sum(list)
(if (null list)
0
(+
(first list)
(sum (rest list))
)
)
)
You can define your custom function to calculate the sum of a list passed to it.
(defun sum (lst) (format t "The sum is ~s~%" (write-to-string (apply '+ lst)))
EVAL: (sum '(1 4 6 4))
-> The sum is "15"
This ought to do the trick:
(defun sum-list (list)
(if list
(+ (car list) (sum-list (cdr list)))
0))
[source]
Edit: Here is another good link that explains car and cdr - basically they are functions that allow you to grab the first element of a list and retrieve a new list sans the first item.
(eval (cons '+ '(1 2 3))) -- though not as good as 'reduce'
(insert (number-to-string (apply '+ '(1 2 3))))