# Writing functions and reading them again

**URL:** <https://racket.discourse.group/t/writing-functions-and-reading-them-again/2938>\
**Category:** Questions & Answers\
**Created:** [May 28, 2024, 4:51pm UTC](https://racket.discourse.group/t/writing-functions-and-reading-them-again/2938 "2024-05-28T16:51:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendrikboom3](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hendrikboom3/32/2748_2.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)\
**Post date:** [May 28, 2024, 4:51pm UTC](https://racket.discourse.group/t/writing-functions-and-reading-them-again/2938/1 "2024-05-28T16:51:49Z")

</div>

I have a data structure that contains Racket functions.  
I would like to be able to write it out to a file, and, later, read it in again -- a save-and-restore operation.  
But as far as I know, it is impossible to write out a Racket function (yes, it's a closure with its own environment) to an output file in such a way that it can be read in again.

Is this true?  
I do want to write something that, when read in, will be the same data structure, containing the same functions whose environments are also part of the read-in data structure.  
Just writing out and read-evalling the source code only will not suffice.

I suspect what I want is not available in Racket.

---

<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:** [May 28, 2024, 5:17pm UTC](https://racket.discourse.group/t/writing-functions-and-reading-them-again/2938/2 "2024-05-28T17:17:14Z")

</div>

An arbitrary closure is not serializable.

However, you might be able to use

```
https://docs.racket-lang.org/web-server/stateless.html

```

from the web-server.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [May 28, 2024, 8:07pm UTC](https://racket.discourse.group/t/writing-functions-and-reading-them-again/2938/3 "2024-05-28T20:07:13Z")

</div>

Yes, [`web-server/lang/serial-lambda`](https://docs.racket-lang.org/web-server-internal/closure.html) provides `serial-lambda` and `serial-case-lambda` forms that create [serializable](https://docs.racket-lang.org/reference/serialization.html) closures. It can be used from `#lang racket` without any of the other web server infrastructure.

An important caveat is that, for serialization to work, the values closed over by the closure must also be serializable with `racket/serialize`.
