1; SketchyLISP Library
2; Copyright (C) 2005,2006,2007 Nils M Holm. All rights reserved.
3; See the file LICENSE of the SketchyLISP distribution
4; for conditions of use.
5
6; ---name---
7; natural
8
9; ---conformance---
10; SketchyLISP Core
11
12; ---purpose---
13; Convert an integer or natural number to a natural number.
14; Only non-negative integers can be converted to natural.
15
16; ---args---
17; A - number
18
19; ---keywords---
20; NATURAL function, numeric, numbers, digit, natural
21
22; ---see-also---
23; digits, integer, natural?
24
25; ---example---
26; (natural +123) => 123
27
28(define natural #t)
29
30(require "digits.scm")
31(require "list.scm")
32(require "integer.scm")
33
34; ---code---
35(define (natural x)
36  (letrec
37    ((_natural
38       (lambda (list-x)
39         (cond ((eq? (car list-x) '+) (cdr list-x))
40           ((eq? (car list-x) '-)
41             (bottom (list 'natural x)))
42           (else list-x)))))
43    (list->integer (_natural (integer->list x)) #t)))
44