David Christiansen's answer was very detailed and thorough, but I wanted to
add one more. If I understand it, your goal is to take an arbitrary
(possibly nested) list of strings and concatenate all the strings. This
will do that:
(define lst '("foo" ("bar") ( ("baz") "jaz") ))
(string-append (flatten lst)) ;; returns "foobarbazjaz"
'flatten' takes any S-expression (loosely, any list) and turns it into a
single one-level list. For example, all of the following produce '(a b c d
(flatten '(a b c d e))
(flatten '((a b) (c d) e))
(flatten '( (((a (b (c d) e)))) ))
(flatten '(a b (c ((d e))) ))
[Note that I used symbols instead of strings so as to avoid typing lots of
" characters, but same thing.]
(string-append (flatten lst)) will fail if there is anything in lst that is
not a string (e.g. a symbol, number, etc) To fix that you could do the
(define (to-str x)
(cond ((number? x) number->string)
((symbol? x) symbol->string)
(...other clauses as desired...)
This is a function that takes one item, determines what type it is, and
then returns a function that will convert that type to a string.
'identity' is a function that takes any one item and returns that item
unchanged -- in this case it's being used as 'string->string'.
Now that you have to-str you can then take the function that it returns and
use it. For example, try doing the following in the Racket REPL:
(define converter (to-str "string_foo")) ;; converter is now the
(converter "string_foo") ;; returns "string_foo"
(define converter (to-str 'symbol_foo)) ;; converter is now the function
(converter 'symbol_foo) ;; returns "symbol_foo" [which is a string]
(define converter (to-str 7)) ;; converter is now the function
(converter 7) ;; returns "7" [which is a string]
There are more types <https://docs.racket-lang.org/guide/datatypes.html>
than I've included, but this should make the point.
At this point you can handle lists that contain non-string items:
(define lst '("foo" (bar "baz" 7) "jaz"))
(string-append (flatten lst)) ;; throws an exception because 'bar isn't a
(string-append (map to-str (flatten lst))) ;; returns "foobarbaz7jaz"
Hope this helps,
On Thu, Oct 13, 2016 at 8:18 AM, Gregor Kopp <gregorkop...@gmail.com> wrote:
> I'm very new to Racket and the lisp family generally, and very slowly
starting to get an idea how everything works together.
> Now my very first question ;)
> If I have a list of string's, how can i recursivly iterate over that list
and compose a single string out of the elements of that list?
> I prepared a simple example, so hopefully you can understand what I'm
> (define mylist '("hello" "darkness" "my" "old" "fiend"))
> (for-each (lambda (elem)
> (displayln elem)
> It prints out every element of mylist as I expect in a new line.
> But what if I want to collect all strings into one single string, so that
I can get "hellodarknessmyoldfriend"?
> I think I can use string-append, right?
> But how could I do that? Is there a idiomatic way to do that in Racket?
> Thank you very much for every input.
> You received this message because you are subscribed to the Google Groups
"Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
For more options, visit https://groups.google.com/d/optout.