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

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

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

Postby stf » Tue May 06, 2014 11:22 pm

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
stf
 
Posts: 1
Joined: Tue May 06, 2014 3:21 pm

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

Postby saulgoode » Fri May 09, 2014 10:08 am

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.
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am


Return to Homework

Who is online

Users browsing this forum: No registered users and 1 guest

cron