Skip to content

Commit 7bb1a7e

Browse files
jcubicarthurgleckler
authored andcommitted
add list of digits into integer recipe
1 parent 0a251b0 commit 7bb1a7e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Convert list of digits into integer
2+
3+
## Problem
4+
5+
I have a list of integers that are digits of a number, the digits may start
6+
with symbol `-`. So this is the reverse operation of the function `integer->list`
7+
from recipe [Convert integer to list of digits](/convert-integer-to-list-of-digits/).
8+
9+
## Solution
10+
11+
### Using string->number and number->string
12+
13+
```scheme
14+
(define (list->integer lst)
15+
(string->number (apply string-append
16+
(map (lambda (item)
17+
(if (and (symbol? item) (symbol=? item '-))
18+
"-"
19+
(number->string item)))
20+
lst))))
21+
```
22+
23+
Credit: [Jakub T. Jankiewicz](https://jcubic.pl/me)
24+
25+
## Usage
26+
27+
Testing if the both functions return original value
28+
29+
```scheme
30+
(let ((numbers '(1000 -1234)))
31+
(for-each (lambda (number)
32+
(display (eq? number (list->integer (integer->list number))))
33+
(newline))
34+
numbers))
35+
;; ==> #t
36+
;; ==> #t
37+
```

0 commit comments

Comments
 (0)