Article View: comp.lang.prolog
Article #149How to get the n'th element of a list
From: Jostein Baustad
Date: Tue, 07 Apr 1992 17:29
Date: Tue, 07 Apr 1992 17:29
28 lines
1947 bytes
1947 bytes
I have a Prolog-predicate that gives me the n'th element of a list: % list_index(List,Index,Element) list_index([Head|_],1,Head). list_index([_|Tail],Index,Elt) :- Index>1, NewIndex is Index-1, list_index(Tail,NewIndex,Elt). This works fine for finding the element when the list and the Index are known (instanciated), but I also want to use the same predicate to find the index (that is the first one) when the list and the element are known. My predicate doesn't work because of the 'is'-operator. Of course, I can use two different predicates, but I have a feeling that it is possible with only one predicate and some fine Prolog-code. Any suggestions anyone? Thanks in advance. Jostein. -- ******************************************************************************* * Jostein Baustad * E-mail : baustad@idt.unit.no * * Norwegian Institute of Technology * baustad@solan.unit.no * * Division of CS and Telematics * "Reality is just a convenient * * Information and Knowledge Systems * measure for complexity." * ******************************************************************************* * Queen's Park Rangers F.C. - SuperHoops ! * *******************************************************************************
Technical Details
Message-ID:
<1992Apr7.172958.5219@ugle.unit.no>Path:
arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!hri.com!snorkelwacker.mit.edu!bloom-beacon!eru.mt.luth.se!lunic!sunic!ugle.unit.no!sigyn.idt.unit.no!baustadShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!hri.com!snorkelwacker.mit.edu!bloom-beacon!eru.mt.luth.se!lunic!sunic!ugle.unit.no!sigyn.idt.unit.no!baustad From: baustad@idt.unit.no (Jostein Baustad) Newsgroups: comp.lang.prolog Subject: How to get the n'th element of a list Message-ID: <1992Apr7.172958.5219@ugle.unit.no> Date: 7 Apr 92 17:29:58 GMT Article-I.D.: ugle.1992Apr7.172958.5219 Sender: news@ugle.unit.no (NetNews Administrator) Reply-To: baustad@idt.unit.no (Jostein Baustad) Followup-To: comp.lang.prolog Organization: Norwegian Institue of Technology Lines: 28