# Map on nested lists

**URL:** <https://racket.discourse.group/t/map-on-nested-lists/3769>\
**Category:** General\
**Created:** [June 2, 2025, 2:01pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769 "2025-06-02T14:01:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [June 2, 2025, 2:01pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/1 "2025-06-02T14:01:09Z")

</div>

I was in need of applying a function to a list of values that contains sub-lists. `map` can not works on nested lists and i can not find a procedure in the scheme library or SRFI so i wrote my own :

```scheme
(module map-nested racket/base

  (provide map-nested )

  ;; (map-nested sin '(0.1 0.2 (0.3) 0.4))
  ;; '(0.09983341664682815 0.19866933079506122 (0.29552020666133955) 0.3894183423086505)

  (define (map-nested f L)

    (define (map-nested-aux x)
      (if (list? x)
	      (map map-nested-aux x)
	      (apply f (list x))))

    (map map-nested-aux L))

) 

```

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [June 2, 2025, 3:28pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/2 "2025-06-02T15:28:33Z")

</div>

Check Shawn's collection of tree functions.

> **[Soup: A library of useful routines](https://docs.racket-lang.org/soup-lib/index.html#%28part._.Tree_functions%29)**

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [June 2, 2025, 8:25pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/3 "2025-06-02T20:25:27Z")

</div>

yes map-tree or walk-tree should make it, map-tree i suppose, but haven't test it as my solution is already good.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [June 2, 2025, 10:09pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/4 "2025-06-02T22:09:28Z")

</div>

`leaf-map` in particular:

```scheme
> (require soup-lib/tree)
> (leaf-map sin '(0.1 0.2 (0.3) 0.4))
'(0.09983341664682815
  0.19866933079506122
  (0.29552020666133955)
  0.3894183423086505)

```

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [June 3, 2025, 7:14am UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/5 "2025-06-03T07:14:28Z")

</div>

oh yes ,it is true it is exactly and only the leaf (leaves ?) i'm aiming.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [June 3, 2025, 8:09pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/6 "2025-06-03T20:09:07Z")

</div>

FWIW, Instead of `(apply f (list x))`, `(f x)` should work and be more performant (since the argument list is fixed, we don't need the indirection of `apply`).

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [June 3, 2025, 9:20pm UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/7 "2025-06-03T21:20:27Z")

</div>

> [@benknoble](#):
>
> `(f x)` should work and be more performant

😮 yes, i corrected the code (i do not know why i overcomplexified my code,sometimes things are so simple, we do not see them 😅)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [June 6, 2025, 10:36am UTC](https://racket.discourse.group/t/map-on-nested-lists/3769/8 "2025-06-06T10:36:43Z")

</div>

and another people outside Racket discourse provide me with this more concise solution:

```scheme
(define (map-nested f L)

    (if (list? L)
      (map (lambda (x) (map-nested f x))
	       L)
      (f L)))

```
