Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-20-2006, 07:41 AM   #1 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
Help in Lisp sorting

Does anyone know about LISP Programming

I am trying to make this program where I have 3 types of cars, which are ford bmw merc

ford has focus and fiesta BMW has 1series and 2series and the merc has SK100 and SK200

well basically i am trying to put them in the correct list for example focus will go into ford list and SK100 will go to the merc list

at the moment to store the data i will use defparameter

example

(defparameter *cars* ' ( focus fiesta 1series 2series SK100 SK200))

so now i want to create a funtion where the cars will go into the correct list, so it will sort the cars into an orgranised lisp when I ran the function. so the cars should be in a randam list but when i ran the function it should sort it into the correct list

(test-a-car 2) the 2 means 2 cars pair a list

ford (fiesta focus)
Merc (SK100 SK200)
BMW (1series 2series)

I have done all the searching in the Internet but I cant find nothing I have tried loads of things but i just hate lisp so please guide me somehow
coolman is offline   Reply With Quote
Old 03-20-2006, 10:05 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Lisp is god's programming language.

You could make an a-list that relates each car to it's manufacturor (Why you would have an exercise relating cars in a language with a primitive called CAR is beyond me). Anyway, an a-list relates data like this (key1 value1 key2 value2 ...) . There's a function for finding the value of a key called GETF. so for this example you could do something like this.
Code:
(defparameter *models* '(fiesta ford sk100 merc focus ford))
(getf *models* 'fiesta)
=> FORD
(getf *models* 'sk100)
=> MERC
(getf *models* 'focus)
=>FORD
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-21-2006, 12:55 AM   #3 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
how do i make dat to a defun so i can ran it

say i got a list full of ramdam cars and they are jumblbed up but i now wanana create a funtion where it will put them in the correct list

(test-a-car 2)

when ran this function it will automactically display it in organised list so the right car will go in the right and it will display the list like


I want it to display a list
ford (focus fieasta)
Merc (SK100 SK200)
coolman is offline   Reply With Quote
Old 03-21-2006, 01:33 AM   #4 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
loop in lisp

I was thinking of using a loop where i will have type of manfacture is ford(key1) and the car is focus (value1) den i would have another list called ford and that would match with the manfacture ford(key1) and therefore the focus value will go into that new ford list

but the only way i can make dat work is if i use loop but i cant seem to make it work
coolman is offline   Reply With Quote
Old 03-21-2006, 02:56 AM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
LOOP is a bitch, plain and simple. It's easier to look at this as a MAP problem i believe. MAP is like foreach for list elements and vectors. So assuming you have an a-list like I descibed above that relates models to manufacturers, you could then use MAP to run over a list of manufacturers and use REMOVE-IF-NOT to select only the models of that manufacturer.
REMOVE-IF-NOT takes a predicate function as it's first parameter so that's where we want to test and see if the result of the :key expression is equal to the manufacturer we're looking at. Since there is no built in function to do this and it is too specific to define a new function for, we can use the LAMBDA function to generate a new anonymous function. The :key function is evaluated for each element REMOVE-IF-NOT examines before it goes to the predicate function. In this case we want the manufacturor of the model so we look it up from the a-list.
Code:
(defparameter *models* '(fiesta ford sk100 merc focus ford 1series bmw))

(defun order-models (model-list manufacturer-list)
  (map 'list
       (lambda (m)
	 (list m (remove-if-not
		  (lambda (y) (equal y m))
		  model-list
		  :key (lambda (x) (getf *models* x)))))
       manufacturer-list))
(order-models '(fiesta sk100 focus 1series) '(bmw ford merc))
=>
((BMW (1SERIES)) (FORD (FIESTA FOCUS)) (MERC (SK100)))
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-21-2006, 08:28 AM   #6 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
ok dats cool (test-a-car 3)

but i want another defun top of that where i will have (test-a-car 3)

and the 3 means 3 car pair list so if ford has more cars like feasta focus mondeo ka den the output should be

ford (focus fiesta mondeo) den another ford list cos u can only have 3 cars a list ford (ka)

c wat i mean
coolman is offline   Reply With Quote
Old 03-21-2006, 12:25 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Well the version I gave you is length independent, so why don't you write another function that splits lists which are too long?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-21-2006, 12:34 PM   #8 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
one more i promise

Now I want to add a bit more detail of the cars like pricing

so focus £9999 how can add those details cos without damaging the program
coolman is offline   Reply With Quote
Old 03-21-2006, 12:48 PM   #9 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
You could make the values of the a-list lists, so that each key references a list
Code:
(defparameter *models* '(fiesta (ford $200 fast) focus (ford $300 slow)))
(getf *models* 'fiesta)
=> (FORD $200 FAST)
(cadr (getf *models* 'fiesta))
=> $200
(caddr (getf *models* 'fiesta))
=> FAST
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Optimizing a Tokenizer in Lisp teknomage1 All Other Coding Languages 1 11-08-2005 07:44 PM
Sorting Arrays [OCS]Midnight Standard C, C++ 1 04-20-2005 04:12 PM
sorting multi dimensional arrays sde PHP 4 10-01-2003 05:52 PM
New Sorting NUM from Technobard sde Java 1 09-17-2002 06:26 PM


All times are GMT -8. The time now is 01:29 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting