# Turning a list into values

**URL:** <https://racket.discourse.group/t/turning-a-list-into-values/2694>\
**Category:** Questions & Answers\
**Created:** [February 1, 2024, 4:10am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694 "2024-02-01T04:10:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markshead](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/markshead/32/1334_2.png) [@markshead](https://racket.discourse.group/u/markshead)\
**Post date:** [February 1, 2024, 4:10am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694/1 "2024-02-01T04:10:57Z")

</div>

Is there a way to turn a list into values. So something like the magic function in this example:

```scheme
(define-values (name age) (magic '("Bob" 25)))

```

---

<div class="post-metadata">

**Author:** ![JustinZed](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@JustinZed](https://racket.discourse.group/u/JustinZed)\
**Post date:** [February 1, 2024, 4:33am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694/2 "2024-02-01T04:33:15Z")

</div>

You can do this with pattern matching.

#lang racket

```scheme
(require racket/match)

(define (magic lst)
  (match lst
      [(list a b) (values a b)]
      [else lst]))

```

Welcome to DrRacket, version 8.11.1 [cs].  
Language: racket, with debugging; memory limit: 4096 MB.

> (define-values (name age) (magic '("Bob" 25)))  
> name  
> "Bob"  
> age  
> 25

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [February 1, 2024, 4:33am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694/3 "2024-02-01T04:33:01Z")

</div>

I think what you really are looking for is pattern matching. If you use `#lang racket`, you can do:

```scheme
(match-define (list name age) '("Bob" 25))

```

But if you really want to use the multiple values approach (which is inefficient compared to the pattern matching approach), you could do:

```scheme
(define-values (name age) (apply values '("Bob" 25)))

```

---

<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:** [February 1, 2024, 5:12am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694/4 "2024-02-01T05:12:07Z")

</div>

Disclaimer: Blatant self promotion ahead

Using the implementation of [SRFI-210: Procedures and Syntax for Multiple Values](https://srfi.schemers.org/srfi-210/srfi-210.html), from my [`extra-srfi-libs`](https://docs.racket-lang.org/extra-srfi-libs/index.html) collection:

```scheme
Welcome to Racket v8.11.1 [cs].
> (require srfi/210)
> (define-values (name age) (list-values '("Bob" 25)))
> name
"Bob"
> age
25

```

(Though I agree with everyone else that using pattern matching is likely a better option for this trivial case.)

---

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [February 6, 2024, 4:19am UTC](https://racket.discourse.group/t/turning-a-list-into-values/2694/5 "2024-02-06T04:19:53Z")

</div>

A third possibility is

```scheme
#lang racket
(define-values (name age) (apply values'("Bob" 25)))
name ; ==> "Bob"
age ; ==> 25

```
