evaluation

F# lazy eval from stream reader?

只谈情不闲聊 提交于 2019-12-10 15:18:45
问题 I'm running into a bug in my code that makes me think that I don't really understand some of the details about F# and lazy evaluation. I know that F# evaluates eagerly and therefore am somewhat perplexed by the following function: // Open a file, then read from it. Close the file. return the data. let getStringFromFile = File.OpenRead("c:\\eo\\raw.txt") |> fun s -> let r = new StreamReader(s) let data = r.ReadToEnd r.Close() s.Close() data When I call this in FSI: > let d = getStringFromFile(

how to get name of data.frame from list passed to function using lapply

不想你离开。 提交于 2019-12-10 14:24:10
问题 I have function which I want to extend with ability to save results to csv file. The name of csv file should be generated based on data.frame name passed to this function: my.func1 <- function(dframe, ...){ # PART OF CODE RESPONSIBLE FOR COMPUTATION # ... # PART OF CODE WHERE I WANT TO STORE RESULTS AS CSV csv <- deparse(substitute(dframe)) csv } When I call this function following way then the name of dataset passed to this function is interpreted correctly: > my.func1(mtcars) [1] "mtcars"

Disambiguation of expressions with neighboring operators of different associativity and same precedence

旧巷老猫 提交于 2019-12-10 11:02:00
问题 Say I have an expression as follows (where ⨁ and ⨂ are binary operators which have the same precedence level but not the same associativity): x ⨁ y ⨂ z Would y belong to ⨁ or ⨂ , and based on what criteria? 回答1: According to the Edsgar Dijkstra's Shunting-yard algorithm if neighboring two operators in an expressions have the same precedence level then the expression is disambiguated based on the associativity of the second operator. If the second operator is left associative then the operand

applicative-order/call-by-value and normal-order/call-by-name differences

前提是你 提交于 2019-12-10 10:38:54
问题 Background I am learning the sicp according to an online course and got confused by its lecture notes. In the lecture notes, the applicative order seems to equal cbv and normal order to cbn. Confusion But the wiki points out that, beside evaluation orders(left to right, right to left, or simultaneous), there is a difference between the applicative order and cbv: Unlike call-by-value, applicative order evaluation reduces terms within a function body as much as possible before the function is

Forced strictness for lists in haskell

喜你入骨 提交于 2019-12-10 01:32:12
问题 I made really time consuming algorithm which produces a short string as the result. When I try to print it (via putStrLn) it appears on the screen character by character. I did understand why that happened, and I tried to force evaluation of the string before actual printing. myPrint !str = putStrLn str But this help very little. When I ran the program in debug I noticed that the !str forced evaluation only for the first character. Does anyone know why is that, and how to deal with this? 回答1:

Evaluating Expression at Runtime

拈花ヽ惹草 提交于 2019-12-09 18:31:47
问题 I have a C# Console Application project. I have a logical expression that is stored in database as nvarchar. For example, the stored expression is: ((34 > 0) || (US == ES)) && (4312 = 5691) While my application running, I want to retrieve the expression and evaluate it so that the result will be true or false. How can I do it at runtime? 回答1: Here's a rather unusual solution, involving JScript: Create a JScript class with the following code: public class JsMath { public static function Eval

How does seq force functions?

不问归期 提交于 2019-12-09 04:13:26
问题 Background This question arises from a challenge Brent Yorgey posed at OPLSS: write a function f :: (Int -> Int) -> Bool that distinguishes f undefined from f (\x -> undefined) . All of our answers either used seq or something like bang patterns that desugar into seq . For example: f :: (Int -> Int) -> Bool f g = g `seq` True *Main> f undefined *** Exception: Prelude.undefined *Main> f (\x -> undefined) True The GHC commentary on seq says that e1 `seq` e2 used to desugar into case e1 of { _ -

Call by name vs call by macro expansion

邮差的信 提交于 2019-12-08 19:16:26
问题 In non strict evaluation languages, what are the differences and advantages/disadvantages of using call by name vs call by macro expansion ? Could you provide an example that explains both evaluation strategies? Thanks! 回答1: Call By Name: Call by name is an evaluation strategy where the arguments to a function are not evaluated before the function is called—rather, they are substituted directly into the function body (using capture-avoiding substitution) and then left to be evaluated whenever

F#: how to evaluate a “seq” to get all its values eagerly?

淺唱寂寞╮ 提交于 2019-12-08 17:35:58
问题 We know that in F#, seq is lazy evaluated. My question is, if I have a seq with limited number of values, how to convert it into some data type that contains all its value evaluated? > seq { for i in 1 .. 10 do yield i * i };; val it : seq<int> = seq [1; 4; 9; 16; ...] Thanks a lot. 回答1: The answer from @Carsten is correct: you can use Seq.toArray or Seq.toList if you wish to convert lazily evaluated sequences to lists or arrays. Don't use these function to force evaluation, though. The most

short circuiting and parenthesis

自闭症网瘾萝莉.ら 提交于 2019-12-08 17:02:15
问题 Does it matter how I group subexpressions when dealing with a single short-circuiting operator? a && b && c && d a && (b && (c && d)) (a && b) && (c && d) ((a && b) && c) && d Are the above expressions equivalent? 回答1: It is relatively easy to prove the equivalence for two simplified cases of three subexpressions: a && (b && c) --> a && bc // bc is a shorthand for b && c Here, a will be evaluated first. If it is false, short circuiting will prevent the evaluation of bc . If it is true, bc