Step-by-step example of a lazy-seq - clojure

I've got a very hard time understanding of lazyness work and how the cache is working.
I think that a step-by-step example of a lazy-seq at work could really help here. For example I've read the following question:
Clojure lazy sequence usage
but it's still not clear.
My question would be how the call determines if another call is "equal" to a cached call and how long does it stays in the cache? I tried to (source lazy-seq) but apparently it's in Java land so I'm out of luck here.
For a simple lazy-seq, taking just one argument (like say the list of powers of two), what if I call it with 5 and then 8? Are just these two values cached?
And what is the point in creating and caching an infinite list to get an infinite structure if I'm going to ruin the memory by caching every input I already called the lazy function with?
Because it says it's caching the result on every subsequent calls... With an 's'.
1: result for argument being '1' cached 2: result for argument being
'2' cached 3: result for argument being '3' cached ... 230: I
counted up to 230 and it's great because I'm lazy and all, but now
there's a 2**30 cache in memory caching all the previous calls for all
the subsequent calls.
or is it just the last call that is cached?
What if I write a lazy function taking trees as arguments? Does it run equals? on the argument passed to know if there needs to be a new evaluation?
Can this behavior somehow be traced at run-time?

The 'cache' in a lazy sequence is not a mutable cache that expires things as you would use in a webapp, it is a cache of size one and there is one in each cell in the list. that 'cache' either contains a value, or the code to compute the value, and never both. once it computes the value it caches the value (in that cell/entry) and if anyone reads the cell again it gives them the value directly instead of calling the code.
here is a simplified imaginary repl session to illustrate the point:
user> (def a (range))
a = [code-for-rest]
user> (first a)
a = [code-for-first, code-for-rest]
a = [0, code-for-rest]
result=> 0
user> (first a)
a = [0, code-for-rest]
result=> 0
user> (nth a 10)
a = [0]->[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]->[9, code-for-rest]
result=> 4
In this example each cell initially contains (and this is a simplification to illustrate just this point) the code to generate the value and the code to generate the rest of the list (or nil if this is the end of the list). once that cell is realized (made unlazy) then it replaces it's contents with the actual value, so it now contains the value and the code to generate the rest of the sequence. When the next cell in the list is read it will first be generated by code-for-rest (as contained in the cell) then the code-for-nth in the new cell will produce the value for that cell.

Here you have a toy example that shows what's happening at run-time:
(defn times-two[number]
(print "- ")
(* 2 number))
(def powers-of-two (lazy-cat [1 2] (map times-two (rest powers-of-two))))
(println (take 10 powers-of-two))
(println (take 12 powers-of-two))
The output should be:
(1 - 2 - 4 - 8 - 16 - 32 - 64 - 128 - 256 512)
(1 2 4 8 16 32 64 128 256 - 512 - 1024 2048)

Related

How to use Map/Reduce in Scheme?

So I have a homework assignment for my CS course covering Scheme procedures. We just started learning the language last week, so I'm lost on how to answer this. I know the map procedure can get the dot product of two lists, but I didn't think reduce was needed to get the product. Below is example code he gives that reverses a list along with the question on a function convolution and dot product. I am also confused on the equation of the list y and how to actually read it. Any help would be greatly appreciated.
(define (reverse lis)
(if (null? lis)
'()
(append (reverse (cdr lis))
(list (car lis)))))
Sketch a function convolution in Scheme that computes the dot product of a list x with the reverse of a list y:
You may use a map/reduce approach, iterate over the list and add each product using a recursive call, or use any approach you wish.
It's probably way to late, but here it is:
Basically Sigma in the equation can be calculate with reduce (reduce + list-of-values) but to simply sum all the items you don't need reduce because you can use (apply + list-of-values). The map part can be use to calculate the list of numbers from two lists. NOTE that Scheme don't define reduce but you can use:
(fold-left + 0 '(1 2 3))
where 0 is inital value
Map work like this:
You can pass single list:
(map square (list 1 2 3))
But in fact map accept any number of lists as arguments:
(map * (list 1 2 3) (list 3 4 5))
and this will return the list of 3 elements where 1st item in first is list is multiplied with first item in second list. So it's like joining two lists and return single list.
To get proper list as output you simply need to reverse the given lists and pass it as one of the argument with orgiginal list.
And then sum up the resulting list. You should write nice function for that:
(define (dot l)
...)
Now you should be able to write the dot function yourself. Sorry but you will never learn if you will give the code itself. Everything is explained.

clojurecademy, thread macro challenge

I'm currently working through clojurecademy and came across this problem which I can't seem to solve -
(= (#_blank (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) (#_blank))
5)
I only need to enter enough to fill in the blank, at first I thought it was just asking me to write what these functions would return, which I think would be (1 2 3 4 5) but that's not correct, I can't figure out why there is a 5 passed in the the equal function, I was thinking maybe I needed to add a function that references 5 from the returned list but I'm not sure how to do that either (without a defined variable) I could be way off...
This is clojurecademy, Problems - Elementary question number 23
The challenge is to make each expression return 5.
As = can take multiple values for equality, Clojure Academy have included 5 within the check too ensure the two expresions you need to complete also return 5.
I would recommend trying to get this expression to return 5
(#_blank (sort (rest (reverse [2 5 4 1 3 6]))))
then work on the next expression to return 5.

Clojure - Inclusive Range

I am using Clojure to do the following task -
Write a function named get-divisors which takes a number n as input and returns the all the numbers between 2 and √𝑛 inclusive
I have this code so far, that seems to be working as expected:
(defn get-divisors [n]
(str (range 2 (Math/sqrt n))))
The user inserts and input and the code shall display all numbers between 2 and the square root of that particular number. (I know! get-divisors is a horrible name for the function)
I type (get-divisors 101) I get the following output
"(2 3 4 5 6 7 8 9 10)" which is correct.
However, the issue is when I use the number 4 I get a result of nil or () when I should in-fact get 2. Or when I enter 49 I should get all numbers between 2 and 7 but I only get all the numbers between 2 and 6.
I have searched online for some information. I am new to Clojure however, the information on this programming seems to be scarce as opposed to the likes of Java, JavaScript. I have read another thread which was based on a similar situation to mind, however, the suggestions/answers didn't work for me unfortunately.
I would appreciate any help. Thank you.
Please see the Clojure CheatSheet. range does not include the upper bound. So, in general, you probably want something like
(range 2 (inc n))
or in your case
(range 2 (inc (Math/floor (Math/sqrt n))))
Also check out http://clojure.org

clojure - lazy sequence internal implementation

I start to read/work on clojure and for that I start to read in parallel 'Programming Clojure' and 'Practical Clojure' books. I saw there one example of how lazy sequence working and for me was very clear in order to understand how lazy-seq work but unfortunately it doesn't work or at least not how I expect.
here is the example:
(defn square[x]
(do
(println "[current.elem=" x "]")
(* x x))
)
(def var-00 (map square '(1 2 3 5 6 4)))
when I call:
var-00
, I expect that no message to print on console(REPL) but I got the follow result:
([current.elem= 1 ][current.elem= 2 ]1 [current.elem= 3 ]4 [current.elem= 5 ]9 [current.elem= 6 ]25 [current.elem= 4 ]36 16)
this mean that the function map was called even I expect to nothing happen since 'var-00' is just a reference to function 'map'; and more awkward from my point of view, if I call:
(nth var-00 2)
I got:
[current.elem= 1 ][current.elem= 2 ][current.elem= 3 ]9
, and if I call again:
(nth var-00 3)
I got:
[current.elem= 1 ][current.elem= 2 ][current.elem= 3 ][current.elem= 5 ]25;
previous elements(1,2,3) was computed again I my opinion those elements should be 'cached' by first call and now only element 5 should be computed. Did I do something wrong or I didn't fully understand how lazy sequence working in clojure ? As a mention I use IntellijIDEA and LaClojure plugin to run the program.
Thanks Sorin.
Just checked your coed in Clojure REPL, it works fine for me. Every element got printed only once (when it's evaluated the first time).
I even tried your example in Clojure online REPL:
But there is one thing that you got wrong. REPL executes each command and then prints its results, so when you type var-00 REPL resolves the symbol and then, in order to print it, executes the whole lazy sequence:
It have nothing to do with lazy sequences, it's just how REPL works:
Lazy Evaluation dosen't mean that things will be cached. It means that inside a calculation an element will only be evaluated, if it is needed for the result. If an element is needed twice for the result, it might be evalueated twice.
If you want to have automatic caching of elements there is the memoize function, which will return a transformed version of the input function with added caching of results. This is also a easy way to implement dynamic programming

Whats wrong with this Clojure program?

I recently started reading Paul Grahams 'On Lisp', and learning learning clojure along with it, so there's probably some really obvious error in here, but I can't see it: (its a project euler problem, obviously)
(ns net.projecteuler.problem31)
(def paths (ref #{}))
; apply fun to all elements of coll for which pred-fun returns true
(defn apply-if [pred-fun fun coll]
(apply fun (filter pred-fun coll)))
(defn make-combination-counter [coin-values]
(fn recurse
([sum] (recurse sum 0 '()))
([max-sum current-sum coin-path]
(if (= max-sum current-sum)
; if we've recursed to the bottom, add current path to paths
(dosync (ref-set paths (conj #paths (sort coin-path))))
; else go on recursing
(apply-if (fn [x] (<= (+ current-sum x) max-sum))
(fn [x] (recurse max-sum (+ x current-sum) (cons x coin-path)))
coin-values)))))
(def count-currency-combinations (make-combination-counter '(1 2 5 10 20 50 100 200)))
(count-currency-combinations 200)
When I run the last line in the REPL, i get the error:
<#CompilerException java.lang.IllegalArgumentException: Wrong number of args passed to: problem31$eval--25$make-combination-counter--27$recurse--29$fn (NO_SOURCE_FILE:0)>
Apart from the question where the error is, the more interesting question would be: How would one debug this? The error message isn't very helpful, and I haven't found a good way to single-step clojure code, and I can't really ask on stack overflow every time I have a problem.
Three tips that might make your life easier here:
Wrong number of args passed to: problem31$eval--25$make-combination-counter--27$recurse--29$fn (NO_SOURCE_FILE:0)>
Tells you roughly where the error occurred: $fn at the end there means anonymous function and it tells you it was declared inside recurse, which was declared inside make-combination-counter. There are two anonymous functions to choose from.
If you save your source-code in a file and execute it as a script it will give you a full stack trace with the line numbers in the file.
at net.projecteuler.problem31$apply_if__9.invoke(problem31.clj:7)
Note you can also examine the last exception and stack trace from within the REPL by examining *e eg: (.stackTrace *e) The stack trace is at first quite daunting because it throws up all the Java internals. You need to learn to ignore those and just look for the lines that refer to your code. This is pretty easy in your case as they all start with net.projecteuler
You can name your anonymous functions to help more quickly identify them:
(fn check-max [x] (<= (+ current-sum x) max-sum))
In your case using all this info you can see that apply-if is being passed a single argument function as fun. Apply does this (f [1 2 3]) -> (f 1 2 3). From your comment what you want is map. (map f [1 2 3]) -> (list (f 1) (f 2) (f 3)). When I replace apply with map the program seems to work.
Finally, if you want to examine values you might want to look into clojure-contrib.logging which has some helpers to this effect. There is a spy macro which allows you to wrap an expression, it will return exactly the same expression so it does not affect the result of your function but will print out EXPR = VALUE, which can be handy. Also on the group various people have posted full tracing solutions. And there is always the trusty println. But the key skill here is being able to identify precisely what blew up. Once you know that it is usually clear why, but sometimes printouts are needed when you can't tell what the inputs are.
dont have a REPL on me though it looks like:
(defn apply-if [pred-fun fun coll]
(apply fun (filter pred-fun coll)))
takes a list like '(1 2 3 4 5) filters some of them out '(1 3 5)
and then creates a function call like (fun 1 3 5)
and it looks like it is being called (apply-if (fn [x] with a function that wants to receive a list of numbers as a single argument.
you could change the apply-if function to just pass call to the fun (with out the apply) or you could change the call to it to take a function that takes an arbitrary number of arguments.