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
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
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
发送时间: 星期三,
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
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
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
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
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.
{
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
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,
10 matches
Mail list logo