Re: Need help with lists !
- From: Chip Eastham <hardmath@xxxxxxxxx>
- Date: Tue, 13 Nov 2007 18:38:16 -0000
On Nov 13, 4:33 am, mari...@xxxxxxxxx wrote:
I have 3 tasks, but I'm a beginner at Prolog:( Does anyone have any
ideas or examples? Thanks. (sorry for my english).
1. equal(List, Delta):
In the List two adjacent elements doesn't differ more than Delta.
For example: goal: equal ([1,3,5,4,3,2,4,2], 2).
true.
2. insert(S,K,R):
Insert K into the list S. List element before K should be smaller than
K and element after K - bigger than K.
For example: goal: insert([10,2,14,8,1], 13, R).
R = [10,2,13,14,8,1].
3. common (S, E):
Element E is the most common in the list S.
For example: goal: common([a,a,c,a,b,b,a,g],E).
E=a.
Defining recursive predicates in Prolog is similar to
proofs by induction. There are one or more "basis"
cases that are simplest/final case to resolve, and
there are one or more rules which reduce the more
complicated/lengthier cases to simpler/shorter ones.
So it may help to "think backwards" and settle the
simplest cases first. In the exercise where a list
should not have adjacent elements that differ by
more than parameter Delta, you might start by
writing the clauses/facts that express success for
the very shortest lists. Then work out a rule
which reduces the solution of a longer list to a
goals involving a shorter list (preferably the tail
of the original input list).
regards, chip
.
- References:
- Need help with lists !
- From: marijus
- Need help with lists !
- Prev by Date: Java to Lisp/Scheme or Prolog translator/parser?
- Next by Date: Re: WAM Vs. TOAM
- Previous by thread: Re: Need help with lists !
- Next by thread: Java to Lisp/Scheme or Prolog translator/parser?
- Index(es):
Relevant Pages
|
|