理解`评价'功能

时间:2016-07-04 02:54:36

标签: haskell lazy-evaluation ghci operator-precedence

Haskell docs解释Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))函数:

  

在执行生成的IO操作时,强制将其参数计算为弱头正常形式。

CustomMapRenderer

为什么using PROJECT; using PROJECT.UWP; using System.Collections.Generic; using Windows.Devices.Geolocation; using Windows.UI.Xaml.Controls.Maps; using Xamarin.Forms.Maps; using Xamarin.Forms.Maps.UWP; using Xamarin.Forms.Platform.UWP; [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))] namespace PROJECT.UWP { public class CustomMapRenderer : MapRenderer { MapControl nativeMap; CustomMap formsMap; protected override void OnElementChanged(ElementChangedEventArgs<Map> e) { base.OnElementChanged(e); if (e.OldElement != null) { nativeMap = Control as MapControl; } if (e.NewElement != null) { formsMap = (CustomMap)e.NewElement; nativeMap = Control as MapControl; UpdatePolyLine(); } } protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (this.Element == null || this.Control == null) return; if (e.PropertyName == CustomMap.RouteCoordinatesProperty.PropertyName) { UpdatePolyLine(); } } private void UpdatePolyLine() { if (formsMap != null && formsMap.RouteCoordinates.Count > 0) { List<BasicGeoposition> coordinates = new List<BasicGeoposition>(); foreach (var position in formsMap.RouteCoordinates) { coordinates.Add(new BasicGeoposition() { Latitude = position.Latitude, Longitude = position.Longitude }); } Geopath path = new Geopath(coordinates); MapPolyline polyline = new MapPolyline(); polyline.StrokeColor = Windows.UI.Color.FromArgb(128, 255, 0, 0); polyline.StrokeThickness = 5; polyline.Path = path; nativeMap.MapElements.Add(polyline); } } } } 不是弱头范式,即evaluate不等于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 = _

2 个答案:

答案 0 :(得分:10)

您的ys值的类型为IO [Int]。现在,IO是一种抽象类型,在您的情况下可以被认为是RealWorld -> ([Int], RealWorld)。现在这个IO值已经处于弱头正常状态。这就是为什么当你对_进行sprint时将其视为ys

  

为什么:sprint ys不是弱头范式,即_ : _不等于ys

_ : _外部字词不能为IO [Int],因为它不是列表,而是{{1}}类型的值。

答案 1 :(得分:9)

除了Sibi所说的内容之外,还有一种方法可以看出evaluate实际上做了文档所说的内容:

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