I need a subroutine for my program written in scheme that takes an integer, say 34109, and puts it into a list with elements 3, 4, 1, 0, 9. The integer can be any length. Does anyone have a trick for this? I've thought about using modulo for every place, but I don't think it should be that complicated.
相关问题
- how to split a list into a given number of sub-lis
- C#: How do i get 2 lists into one 2-tuple list in
- F#: Storing and mapping a list of functions
- Select first row from multiple dataframe and bind
- Generating powerset in one function, no explicit r
相关文章
- List可以存储接口类型的数据吗?
-
C# List
.FindAll 时 空指针异常 - What is the complexity of bisect algorithm?
- Given a list and a bitmask, how do I return the va
- Why does slice [:-0] return empty list in Python
- Does learning one Lisp help in learning the other?
- Style bullet-list with arrows
- bash: /bin/ls: Argument list too long
Something like this:
As itsbruce commented you can hide helper function inside main one:
to be continued...
I'm not a fan of manual looping, so here's a solution based on unfold (load SRFI 1 and SRFI 26 first):
This returns an empty list for 0, though. If you want it to return
(0)
instead, we add a special case:Of course, you can generalise this for other bases. Here, I implement this using optional arguments, so if you don't specify the base, it defaults to 10:
Different Scheme implementations use different syntaxes for optional arguments; the above uses Racket-style (and/or SRFI 89-style) syntax.
The simplest way I can think of, is by using arithmetic operations and a named
let
for implementing a tail-recursion:Alternatively, you can solve this problem using string operations:
This can be compressed into a single function, but I believe it's easier to understand if we split the problem in subparts as above.
Anyway, the results are as expected: