问题描述:

There is a function that do "shallow flat"?

I mean, like the function flatten but only one () from each expression in the list

For example:

 (shallow flat '((1) (2) ( ( 4 5) 6)))

return

'(1 2 (4 5) 6))

thanks

网友答案:
(apply append '((1) (2) ((4 5) 6)))   ;=> (1 2 (4 5) 6))
(apply append '((1) (2) (((4) 5) 6))) ;=> (1 2 ((4) 5) 6)

You can make it a procedure, ie:

(define (shallow-flatten lst)
  (apply append lst))
网友答案:

As Chris suggested, you can use SRFI 1's concatenate:

(require srfi/1) ; import the library, this is Racket's syntax
(concatenate '((1) (2) ((4 5) 6)))

Alternatively, in Racket you can use append*, check to see if it's available in your interpreter:

(append* '((1) (2) ((4 5) 6)))

Either way, the result is as expected and it wasn't necessary to reinvent the wheel by defining an already existing procedure:

=> '(1 2 (4 5) 6)
相关阅读:
Top