How to use map and apply

How to use map? For example, I have a function
(map string-length
(list “cat” “dog” “fish” “banana”))
I simply do
(list (string-length “cat”) (string-length “dog”) (string-length “fish”) (string-length “banana”))
The answer will be
(list 3 3 4 6)
This is how we use map, we bring all the listed elements into the function and return a new list.

How to use apply? For example, I have a function,
(apply string-append
(list “cat” “dog” “fish” “banana”))
I simply do
(apply string-append
 “cat” “dog” “fish” “banana”)
The answer will be
(catdogfishbanana)

This is how we use apply, we bring all the elements into one function and return it together.

Comments

Popular posts from this blog

Diana's first blog

How to design a function and how to prepare for the course

Mathematics