Clinton
Clinton

Reputation: 23135

Haskell: How to define strict tuples

Lets say I have the following:

f (a, b) = if a == 0 then (0, 0) else (a * b, a / b)

x1 = make_strict (0, undefined)
x2 = (0, undefined)

g f :: (b -> b) -> a -> a

How do define make_strict and g:

g f x = ... f x ...
make_strict x = ...

So that:

g f x1 == undef
g f x2 == (0, 0)

Basically I want to make a strict version of a pair that I can then pass to a function which takes a pair, possibly through a wrapper g if necessary. The particular implementation of f here is just an example, the solution shouldn't rely on it. The point is I can't change f, I can only change g.

Upvotes: 3

Views: 1244

Answers (1)

augustss
augustss

Reputation: 23014

How about

makeStrict (a, b) = seq a (seq b (a, b))

Upvotes: 7

Related Questions