Page 1 of 1

from '((a b c d e)((f g h)(i j k))(l m n o))) to '((a b c d

Posted: Tue May 06, 2014 11:22 pm
by stf
Hi,

i stuck on a problem, a kind of special flatten function :

i want to input: '((a b c d e)((f g h)(i j k))(l m n o)))

and output:

=> '((a b c d e) (f g h)(i j k)(l m n o))

i can't find the solution for this problem and don't know lisp function who can do that..

could you help me please ?

thanks

stf

Re: from '((a b c d e)((f g h)(i j k))(l m n o))) to '((a b

Posted: Fri May 09, 2014 10:08 am
by saulgoode
I'm not aware of any library function that performs that directly, but it can be accomplished fairly easily if you break it down into manageable steps.

It appears that your input is a list in the form:
(item1 item2 item3)
and each of the items will be either a list of atoms:
(atom1 atom2 atom3)
or a list that contains sublists:
(sublist1 sublist2)

First you will need to devise a way to determine whether an item contains any (sub)lists. In Scheme you can achieve this with (member #t (map list? item)) .

You then can loop over each of the items, and based on whether the item contains sublists or atoms, you will either want to append the item to your accumulated result (thus flattening the item by one level), or cons it to the result (leaving it an unflattened list of atoms). This can be accomplished using fold in Scheme or reduce in Lisp.