[clean-list] Can anyone improve this program?

Nick Oosterhof n.n.oosterhof at student.utwente.nl
Sun Jul 10 01:17:35 MEST 2005


Jigang Sun wrote:

>In order to print [(5,0), (6,0), (7,0)], I use a temporary variable (as if) n  (Int type). Can anyone  improve it without using the temporary variable? 
>i.e. the function will be defined as count::[a]->[(a,Int)]
>
>
>count::Int [a]->[(a,Int)]
>count n []=[] 
>count n [x:xs]=[(x, n ): count (n+1) xs]
>
>Start=count 0 [5,6,7]
>  
>
This program prints [(5,0),(6,1),(7,2)], not [(5,0),(6,0),(7,0)]. If
this is what you want, then the following list definition will do the job:

count n xs = zip2 xs [n..]

The zip2 function takes two lists as its arguments, and returns a list
of pairs (with the elements taken together position-wise). The length of
the resulting list is equal to that of the shortest input list, in this
case the length of xs.

Regards,

Nick



More information about the clean-list mailing list