For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2006 > Number of successive number in a list









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Number of successive number in a list
niarvani@csd.auth.gr

2006-12-16, 8:04 am

I want to write a predicate to return the number of the successive
numbers in a list.
For example:

number_of_successive_numbers([1,2,3,4,5]
,Number).
Number = 5.

or

number_of_successive_number([2,5,6,7,9],
Number).
Number = 3.

How can i do that?
Thanks in advance

A.L.

2006-12-16, 7:05 pm

On 16 Dec 2006 03:38:31 -0800, niarvani@csd.auth.gr wrote:

>I want to write a predicate to return the number of the successive
>numbers in a list.
>For example:
>
> number_of_successive_numbers([1,2,3,4,5]
,Number).
>Number = 5.
>
>or
>
> number_of_successive_number([2,5,6,7,9],
Number).
>Number = 3.
>
>How can i do that?
>Thanks in advance


There are some volunteers here who do other people's homeworks. Wait
patiently.

A.L.
Ben

2006-12-18, 8:05 am

Here's something slightly different, it finds the maximum length of
numbers that will follow in succession. (instead of the length of the
sequence of numbers). Though I guess you would just want the answer to
this plus one for anything but an empty list?

Ben

number_of_successive_number(L,M):-
snums(L,0,0,M).

% base case - no elements left
snums([],_,M,M).

snums([A,B|Rest],S,M,Mfinal):-
%is successor
B is A + 1,!,
Ns is S + 1,
snums([B|Rest],Ns,M,Mfinal).

snums([_|Rest],S,M,Mfinal):-
% not succesor
M > S,!,
snums(Rest,0,M,Mfinal);
snums(Rest,0,S,Mfinal).

On Dec 16, 11:38 am, niarv...@csd.auth.gr wrote:
> I want to write a predicate to return the number of the successive
> numbers in a list.
> For example:
>
> number_of_successive_numbers([1,2,3,4,5]
,Number).
> Number = 5.
>
> or
>
> number_of_successive_number([2,5,6,7,9],
Number).
> Number = 3.
>
> How can i do that?
> Thanks in advance


Ben

2006-12-18, 8:05 am

> There are some volunteers here who do other people's homeworks. Wait
> patiently.
>
> A.L.


Damn, I've got my own homework to do... Anyone care to give me an
introduction to an essay on DRM in Trustworthy Computing!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com