weak-head-normal-form

Test if a value has been evaluated to weak head normal form

送分小仙女□ 提交于 2019-12-12 07:32:43
问题 In Haskell, is it possible to test if a value has been evaluated to weak head normal form? If a function already exists, I would expect it to have a signature like evaluated :: a -> IO Bool There are a few places that similar functionality lives. A previous answer introduced me to the :sprint ghci command, which will print only the portion of a value that has already been forced to weak head normal form. :sprint can observe whether or not a value has been evaluated: > let l = ['a'..] >

Weak head normal form and order of evaluation

旧巷老猫 提交于 2019-12-10 19:52:15
问题 I've read lots on weak head normal form and seq. But I'm still have trouble imagining the logic behind Haskell's order of evaluation A common example demonstrating when and how to use but I still don't understand how the common example foldl (+) 0 [1..5000000] can result in a stack overflow. While another fold definition using seq doesn't foldl' _ a [] = a foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs foldl' (+) 0 [0..5000000] From explanations of seq that I've read, authors

Why is a built-in function applied to too few arguments considered to be in weak head normal form?

会有一股神秘感。 提交于 2019-12-04 02:50:27
问题 The Haskell definition says: An expression is in weak head normal form (WHNF), if it is either: a constructor (eventually applied to arguments) like True, Just (square 42) or (:) 1 a built-in function applied to too few arguments (perhaps none) like (+) 2 or sqrt. or a lambda abstraction \x -> expression. Why do built-in functions receive special treatment? According to lambda calculus, there is no difference between a partially applied function and any other function, because at the end we

Understanding the different behavior of thunks when GHCi let bindings are involved

我的梦境 提交于 2019-12-03 05:26:11
问题 I've been playing with some examples from Simon Marlow's book about parallel and concurrent programming in Haskell and stumbled across an interesting behavior that I don't really understand. This is really about me trying to understand some of the inner workings of GHC. Let's say I do the following in the REPL: λ» let x = 1 + 2 :: Int λ» let z = (x,x) λ» :sprint x x = _ λ» :sprint z z = (_,_) λ» seq x () () λ» :sprint z z = (3,3) Ok, this is pretty much what I expected except that z gets

Haskell foldl' poor performance with (++)

假装没事ソ 提交于 2019-11-26 04:56:08
问题 I have this code: import Data.List newList_bad lst = foldl\' (\\acc x -> acc ++ [x*2]) [] lst newList_good lst = foldl\' (\\acc x -> x*2 : acc) [] lst These functions return lists with each element multiplied by 2: *Main> newList_bad [1..10] [2,4,6,8,10,12,14,16,18,20] *Main> newList_good [1..10] [20,18,16,14,12,10,8,6,4,2] In ghci: *Main> sum $ newList_bad [1..15000] 225015000 (5.24 secs, 4767099960 bytes) *Main> sum $ newList_good [1..15000] 225015000 (0.03 secs, 3190716 bytes) Why newList

What is Weak Head Normal Form?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 01:34:38
问题 What does Weak Head Normal Form (WHNF) mean? What does Head Normal form (HNF) and Normal Form (NF) mean? Real World Haskell states: The familiar seq function evaluates an expression to what we call head normal form (abbreviated HNF). It stops once it reaches the outermost constructor (the “head”). This is distinct from normal form (NF), in which an expression is completely evaluated. You will also hear Haskell programmers refer to weak head normal form (WHNF). For normal data, weak head