lazy-evaluation

Can every functional language be lazy?

允我心安 提交于 2021-02-18 21:29:33
问题 In a functional language, functions are first class citizens and thus calling them is not the only thing I can do. I can also store them away. Now when I have a language, which is strict by default, then I am still not forced to evaluate a function call. I have the option to store the function and its parameters e.g. in a tuple for later evaluation. So instead of x = f a b c I do something like x = (f,a,b,c) And later, I can evaluate this thing with something like eval (f,a,b,c) = f a b c

Why does my lazy filtered list in scheme consume so much memory?

雨燕双飞 提交于 2021-02-10 21:54:10
问题 I'm currently learning to use some slightly more advanced features of scheme, and I've hit a road-block with lazy lists. Basically, I'm trying to create an infinite, lazily generated list, and apply a lazy filter on it, and only take a single element. My hope was that this would consume very little memory: the filter looks at only one element at a time, and there's no need to store the previous entries. Here is my attempt at this: (define lazy-inf-seq (lambda (start next) (delay (cons start

How to swap two elements inside of a 2D Array in JavaScript? (Confused about what I’m seeing from console.log in Chrome)

若如初见. 提交于 2021-02-08 20:53:20
问题 I want to swap two arrays inside a 2D array, however JS seems to be doing that before my actual swap is happening. This is for an algorithm I'm working on, providing each possible way to display a list of points. I have tried several ways of doing this, but the key problem keeps returning, and I've managed to crop it down to this peace of code: var points = [[1,2],[10,20],[100,200]]; console.log(points); var a = points[1][0]; var b = points[1][1]; points[1][0] = points[2][0]; points[1][1] =

How to swap two elements inside of a 2D Array in JavaScript? (Confused about what I’m seeing from console.log in Chrome)

拈花ヽ惹草 提交于 2021-02-08 20:51:11
问题 I want to swap two arrays inside a 2D array, however JS seems to be doing that before my actual swap is happening. This is for an algorithm I'm working on, providing each possible way to display a list of points. I have tried several ways of doing this, but the key problem keeps returning, and I've managed to crop it down to this peace of code: var points = [[1,2],[10,20],[100,200]]; console.log(points); var a = points[1][0]; var b = points[1][1]; points[1][0] = points[2][0]; points[1][1] =

Lazy evaluation in Haskell's do notation using the trace function

百般思念 提交于 2021-02-05 05:54:12
问题 I want to know why this "debug message 1" doesn't get printed in this snippet: import Debug.Trace main = do return (trace "debug message 1" ()) trace "debug message 2" (return ()) The second "debug message 2" is printed out, but not "debug message 1". It seems that both expressions are the same. I tried binding the "debug message 1" to a variable, and then using that variable in another place, it did in fact trigger the evaluation and print "debug message 1", but I still don't understand why

Lazy evaluation in Haskell's do notation using the trace function

时光总嘲笑我的痴心妄想 提交于 2021-02-05 05:54:05
问题 I want to know why this "debug message 1" doesn't get printed in this snippet: import Debug.Trace main = do return (trace "debug message 1" ()) trace "debug message 2" (return ()) The second "debug message 2" is printed out, but not "debug message 1". It seems that both expressions are the same. I tried binding the "debug message 1" to a variable, and then using that variable in another place, it did in fact trigger the evaluation and print "debug message 1", but I still don't understand why

observeEvent in insertUI generated in loop

一曲冷凌霜 提交于 2021-02-04 21:09:55
问题 When I create new objects with insertUI in a reactive way, all the observers that I create work perfectly fine, as you can see in the following dummy code: library(shiny) # Define the UI ui <- fluidPage( actionButton("adder", "Add"), tags$div(id = 'placeholder') ) # Define the server code server <- function(input, output) { rv <- reactiveValues() rv$counter <- 0 observeEvent(input$adder,{ rv$counter <- rv$counter + 1 add <- sprintf("%03d",rv$counter) filterId <- paste0('adder_', add) divId <-

observeEvent in insertUI generated in loop

≡放荡痞女 提交于 2021-02-04 21:09:04
问题 When I create new objects with insertUI in a reactive way, all the observers that I create work perfectly fine, as you can see in the following dummy code: library(shiny) # Define the UI ui <- fluidPage( actionButton("adder", "Add"), tags$div(id = 'placeholder') ) # Define the server code server <- function(input, output) { rv <- reactiveValues() rv$counter <- 0 observeEvent(input$adder,{ rv$counter <- rv$counter + 1 add <- sprintf("%03d",rv$counter) filterId <- paste0('adder_', add) divId <-

Why is the strictness-introducing function called seq?

ぐ巨炮叔叔 提交于 2021-02-02 00:30:44
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Why is the strictness-introducing function called seq?

怎甘沉沦 提交于 2021-02-02 00:29:52
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell: