Re: [Haskell-cafe] How to implement nested loops with tail recursion?

2012-09-19 Thread sdiyazg
So how do I force IO actions whose results are discarded (including IO ()) to be strict? main = do s<-newIORef (1::Int) let f :: Int -> Int -> IO Int f 0 !acc = return acc -- note strict accumulator f n !acc = do

Re: [Haskell-cafe] How to implement nested loops with tail recursion?

2012-09-19 Thread sdiyazg
A follow-up question. I still haven't got the monadic version working, and the real use case involves IO actions. I looked at http://www.haskell.org/haskellwiki/Recursion_in_a_monad and adapted the 'tail-recursive' snippet on the page into main = do let f 0 acc = return

Re: [Haskell-cafe] How to implement nested loops with tail recursion?

2012-09-19 Thread sdiyazg
Now I have discovered the right version... main = print (f 1 0::Int) where f i s = (if i<=2 then (f (i+1) (s + g 1 0)) else s) where g j s = (if j<=2 then (g (j+1) (s + i*j)) else s) - 原始邮件 - 发件人: sdiy...@sjtu.edu.cn 收件人: haskell-cafe@haskell.org 发送时间: 星期三,

[Haskell-cafe] How to implement nested loops with tail recursion?

2012-09-19 Thread sdiyazg
I need to implement fast two-level loops, and I am learning using seq to make calls tail-recursive. I write programs to compute main = print $ sum [i*j|i::Int<-[1..2],j::Int<-[1..2]] This program (compiled with -O2) runs twenty times slower than the unoptimized (otherwise the loop gets

Re: [Haskell-cafe] Are there arithmetic composition of functions?

2012-03-21 Thread sdiyazg
Wow, there are so many people interested in this:) After reading the replies and some trail and error, now I think I need to look into Numeric Prelude first. I hadn't known of NP until reading Richard O'Keefe's reply. I will also try purely syntactic expansion with TH, but I haven't used TH seri

[Haskell-cafe] Are there arithmetic composition of functions?

2012-03-19 Thread sdiyazg
By arithmetic I mean the everyday arithmetic operations used in engineering. In signal processing for example, we write a lot of expressions like f(t)=g(t)+h(t)+g'(t) or f(t)=g(t)*h(t). I feel it would be very natural to have in haskell something like g::Float->Float --define g here h::F

Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-03 Thread sdiyazg
Quoting Andrew Coppin : On 02/10/2011 07:15 PM, Du Xi wrote: In C++, the code is inferred from the types. (I.e., if a function is overloaded, the correct implementation is selected depending on the types of the arguments.) In Haskell, the types are inferred from the code. (Which is why type si

Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-03 Thread sdiyazg
Quoting Felipe Almeida Lessa : On Sun, Oct 2, 2011 at 4:26 PM, Edward Z. Yang wrote: What are you actually trying to do? This seems like a rather unusual function. If you're new to the language, most likely you're doing something wrong if you need this kind of function. =) -- Felipe. {

Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-02 Thread sdiyazg
Quoting Richard O'Keefe : On 3/10/2011, at 7:15 AM, Du Xi wrote: I guess this is what I want, thank you all. Although I still wonder why something so simple in C++ is actually more verbose and requires less known features in Haskell...What was the design intent to disallow simple ove

Re: [Haskell-cafe] Is it possible to represent such polymorphism?

2011-10-02 Thread sdiyazg
Finally I got what I meant: class ExpandTuple t where type Result t expand :: t->Result t instance (Integral a)=>ExpandTuple (a,a) where type Result (a,a) = (a,a,a) expand (x,y) = (x,y,1) instance (Integral a)=>ExpandTuple (a,a,a) where type Result (a,a,