<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>This type-checks:</div><div><div><font face="Courier New">#lang typed/racket</font></div><div><font face="Courier New"><br></font></div><div><font face="Courier New">(: log2 (Nonnegative-Real -> Real))</font></div><div><font face="Courier New">(define (log2 x)</font></div><div><font face="Courier New">  (/ (log x) (log 2)))</font></div></div><div>Why don’t you just use this?  </div><br><div><div>On May 3, 2014, at 10:09 AM, Jens Axel Søgaard <<a href="mailto:jensaxel@soegaard.net">jensaxel@soegaard.net</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">2014-05-03 8:43 GMT+02:00 Daniel Prager <<a href="mailto:daniel.a.prager@gmail.com">daniel.a.prager@gmail.com</a>>:<br><blockquote type="cite">I'm having a go at converting a pitch detection algorithm from MATLAB to<br>Typed Racket, and which I'll Open Source if I can get it working, and am<br>after a few pointers, since I'm new to TR and am finding it slow going.<br><br>Examples of the kind of code I'm writing in my fledlging attempts:<br><br>#lang typed/racket<br><br>(define: (log2 [x : Real]) : Real<br>  (define result (/ (log x) (log 2)))<br>  (if (real? result)<br>      result<br>      0.0))<br></blockquote><br>My strategy is write the write the function in normal Racket first:<br><br>  (define (log2 x)<br>      (/ (log x) (log 2)))<br><br>Then to add the type:<br><br>  (: log2 : Real -> Real)<br>  (define (log2 x)<br>      (/ (log x) (log 2)))<br><br>In this case I get a type error, the result is not a Real but a Number.<br>If x is negative, then (log x) is a complex number. I therefore need<br>to insert a an explicit check that x is positive:<br><br>(: log2 : Real -> Real)<br>(define (log2 x)<br>    (cond [(positive? x) (/ (log x) (log 2))]<br>              [else             (error 'log2 "positive number expected")]))<br><br>This version type checks.<br><br><blockquote type="cite">(define: (build-step-vector [min : Real] [step : Real] [max : Real]) :<br>  (Vectorof Real)<br>  (for/vector: : (Vectorof Real)<br>      #:length (ceiling (inexact->exact (/ (- max min) step)))<br>      ([i (in-naturals)])<br>    (+ min (* i step))))<br></blockquote><br>Here I tried this version:<br><br>(: step-vector : Real Real Real -> (Vectorof Real))<br>(define (step-vector min max step)<br>  (for/vector #:length (exact-ceiling (/ (- max min) step))<br>    ([x (in-range min max step)])<br>    x))<br><br>This gave an error, since TR could only infer that the result was a<br>(Vectorof Any).<br>Then I changed it to:<br><br>(: step-vector : Real Real Real -> (Vectorof Real))<br>(define (step-vector min max step)<br>  (for/vector: : (Vectorof Real) #:length (exact-ceiling (/ (- max min) step))<br>    ([x (in-range min max step)])<br>    x))<br><br><blockquote type="cite">Beyond that:<br><br>Can anyone point me to examples of any code before/after converted from<br>MATLAB (or Octave) to TR?<br></blockquote><br>I haven't any experience with either (except reading parts of the<br>documentation).<br>Do you have any particular examples in mind?<br><br><blockquote type="cite">What's a good scalar type to work with for floating point to start with?<br></blockquote><br>Flonum ?<br><br><blockquote type="cite">Which non-scalar type(s) should I favor: vectors, arrays or matrices?<br></blockquote><br>Matrices are just two-dimensional arrays. Therefore if there is a matrix<br>operation missing, mostly likely you can find it in the array section<br>of the documentation.<br><br>If you stick with matrices, you can view vectors as nx1 matrices.<br>This allows you to use the matrix operations on vectors.<br><br>Depending on the operations you need, I'd try out matrices.<br><br><blockquote type="cite">Any general tips to develop facility for this kind of stuff? I feel like I<br>need to go "back to basics", but there seems to be a dearth of examples and<br>tutorials. on math/... beyond the reference materials.<br></blockquote><br>True.<br><br>/Jens Axel<br>____________________<br>  Racket Users list:<br>  <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>