Веб Зображення Новини Групи Блоги Перекладач Gmail Ще »
Групи, які ви переглядали нещодавно | Довідка | Увійти
Головна сторінка Груп Google
Допис з розмови Avoid unnecessary evaluations

Переглянути розібране Показувати лише текст повідомлення

Newsgroups: comp.lang.haskell
From: Florian Kreidler <m...@privacy.net>
Subject: Re: Avoid unnecessary evaluations
References: <787ia8F1h6d4hU1@mid.dfncis.de>
User-Agent: slrn/pre1.0.0-10 (Linux)
NNTP-Posting-Host: 88.70.206.21
Message-ID: <4a1e9b9e$1_2@news.arcor-ip.de>
Date: 28 May 2009 16:11:42 +0200
X-Trace: news.arcor-ip.de 1243519902 88.70.206.21 (28 May 2009 16:11:42 +0200)
Lines: 32
X-Complaints-To: abuse@arcor-ip.de
Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor-ip.de!news.arcor-ip.de!not-for-mail

Frank Poettgen <Frank.Poett...@Post.RxWyTxH-AyAzCyHzEyN.de> schrieb:
> Assume that the evaluation of a function 'expensive' takes a lot of 
> time. Hence I want to avoid unnecessary evaluations of 'expensive'.
>
> expensive :: Int -> (Int,Int)
> expensive     x  =  (x,x)
>
> f :: Int -> Int
> f     x  = fst (expensive x) + snd (expensive x)
>
> Will 'expensive x' be evaluated twice to compute 'f x'?

That will depend on your compiler. Some compilers do common
subexpression elimination, others don't. The reason is that
this transformation can sometimes introduce space leaks. In
your case it wouldn't be dangerous, but you cannot rely on
your compiler recognizing this.

> If so, how can I avoid it?

Rewrite the function by hand:

  f x = fst e + snd e where e = expensive

or, using pattern matching,

  f x = l + r where (l, r) = expensive x

or, more compact but less comprehensible,

  f = uncurry (+) . expensive


Створити групу - Групи Google - Домашня сторінка Google - Правила користування послугою - Заява про конфіденційність і нерозголошення інформації
©2009 Google