<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">All very neat, and it works! Thanks.<br><br>But I copied <br><br>map :: (a -&gt; b) -&gt; [a] -&gt; [b] &nbsp;&nbsp; &lt;==&nbsp; I'm assuming this is correct<br><br>from something called "A Tour of the Haskell Prelude" at<br><br>http://undergraduate.csse.uwa.edu.au/units/CITS3211/lectureNotes/tourofprelude.html#all<br><br>which I was looking at to try and roll my own typing.<br><br><br>My next question:<br><br>My function<br><br>s f ls <br><br>seems much like<br><br>map f ls<br><br>but instead looks like&nbsp;&nbsp; <br><br>s :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; [a]<br><br><br>Clearly, there's more to the picture than meets the eye. Is there a good tutorial on types?<br><br>Michael<br><br><br>--- On <b>Fri, 4/10/09, Joe Fredette <i>&lt;jfredett@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px;
 padding-left: 5px;"><br>From: Joe Fredette &lt;jfredett@gmail.com&gt;<br>Subject: Re: [Haskell-cafe] Sequence differences<br>To: "michael rice" &lt;nowgate@yahoo.com&gt;, "haskell Cafe mailing list" &lt;haskell-cafe@haskell.org&gt;<br>Date: Friday, April 10, 2009, 2:07 AM<br><br><div class="plainMail">So, we can walk through it-<br><br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;s f [] = []<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;s f [x] = [x]<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;s f l = [ a f b | (a,b) &lt;- zip (init l) (tail l)]<br><br>First, we can write some of it to be a little more idiomatic, viz:<br><br>s _ []&nbsp; = []<br>s _ [x] = [x]<br>s f ls&nbsp; = [f a b | (a,b) &lt;- zip (init ls) (tail ls)]<br><br>First, we have a function type, we can tell the variable f is a function because it's applied to arguments in the third case, since it's applied to two arguments, it's binary, so `s :: (a -&gt; b -&gt; c) -&gt; ?` however, from the<br>second case, we know that whatever
 the type of the second argument (a list of some type `a1`) is also the type<br>of the return argument, since the `s` acts as the identity for lists of length less than 2, so<br><br>&nbsp;&nbsp;&nbsp;s :: (a -&gt; b -&gt; a1) -&gt; [a1] -&gt; [a1]<br><br>However, since the arguments for `f` are drawn from the same list, the argument types must _also_ be of type `a1`, leaving us with:<br><br>&nbsp;&nbsp;&nbsp;s :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; [a]<br><br>This is, interestingly enough, precisely the type of foldr1.<br><br>We can write your original function in another, cleaner way though, too, since zip will "zip" to the smaller of the two lengths, so you don't need to worry about doing the init and the tail, so `s` is really:<br><br>s _ []&nbsp; = []<br>s _ [x] = [x]<br>s f ls&nbsp; = [f a b | (a,b) &lt;- zip ls (tail ls)]<br><br>but there is a function which does precisely what the third case does, called "zipWith" which takes a<br>binary function
 and two lists and -- well -- does what that list comprehension does. In fact, it does<br>what your whole function does... In fact, it _is_ your function, specialized a little, eg:<br><br>yourZipWith f ls = zipWith f ls (tail ls)<br><br><br>Hope that helps<br><br>/Joe<br><br>michael rice wrote:<br>&gt; I have a Scheme function that calculates sequence differences, i.e., it returns a sequence that is the difference between the 2nd and the 1st element, the 3rd and the 2nd, the 4th and the 3rd, etc.<br>&gt; <br>&gt; (define s<br>&gt;&nbsp;&nbsp;&nbsp;(lambda (f l)<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;(cond ((null? (cdr l)) '())<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;(else (cons (f (cadr l) (car l))<br>&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;(s f (cdr l)))))))<br>&gt; <br>&gt; where<br>&gt; <br>&gt; (s - '(0,1,3,6,10,15,21,28)) =&gt; (1,2,3,4,5,6,7)<br>&gt; <br>&gt; <br>&gt; I'm thinking the same
 function in Haskell would be something like<br>&gt; <br>&gt; s ::<br>&gt; s f [] = []<br>&gt; s f [x] = [x]<br>&gt; s f l = [ a f b | (a,b) &lt;- zip (init l) (tail l)]<br>&gt; <br>&gt; <br>&gt; but can't figure out what the function typing would be.<br>&gt; <br>&gt; Michael<br>&gt; <br>&gt; <br>&gt; ------------------------------------------------------------------------<br>&gt; <br>&gt; _______________________________________________<br>&gt; Haskell-Cafe mailing list<br>&gt; <a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>&gt;&nbsp;&nbsp;&nbsp;</div></blockquote></td></tr></table><br>