2016-07-04 3 views
2

Die Haskell docs die evaluate Funktion erklären:Verständnis `evaluate` Functiion

Kräfte ihre Argumente in der schwachen Kopf Normalform ausgewertet werden, wenn die resultierende IO Aktion ausgeführt wird.

Prelude Control.Exception> let xs = [1..100] :: [Int]                 Prelude Control.Exception> :sprint xs 
xs = _ 
Prelude Control.Exception> let ys = evaluate xs 
Prelude Control.Exception> :t ys 
ys :: IO [Int] 
Prelude Control.Exception> ys 
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,6Prelu2,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100] 
Prelude Control.Exception> :sprint xs 
xs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, 
     24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45, 
     46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67, 
     68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89, 
     90,91,92,93,94,95,96,97,98,99,100] 
Prelude Control.Exception> :sprint ys 
ys = _ 

Warum ist ys nicht in Weak Kopf Normalform, das heißt :sprint ys nicht gleich _ : _?

Antwort

10

Ihr ys Wert hat eine Art IO [Int]. Jetzt ist IO ein abstrakter Typ und kann in Ihrem Fall als RealWorld -> ([Int], RealWorld) angesehen werden. Jetzt ist dieser IO Wert bereits in schwacher Normalkopfform. Deshalb sehen Sie es als _, wenn Sie sprint darauf tun.

Warum ist ys nicht in Weak Kopf Normalform, das heißt :sprint ys nicht gleich _ : _?

ys äußere Begriff kann nicht _ : _ sein, weil es keine Liste ist, aber es ist Wert vom Typ IO [Int].

9

Zusätzlich zu dem, was von Sibi gesagt wurde, hier ist ein Weg, um zu sehen, dass evaluate tatsächlich tut, was die docs sagen:

GHCi> let xs = [1..100] :: [Int] 
GHCi> :sprint xs 
xs = _ 
GHCi> let a = evaluate xs >> return() 
GHCi> a 
GHCi> :sprint xs 
xs = 1 : _