clojurecademy, thread macro challenge - clojure

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.

Related

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

Is there a simple way to get reader macro string from defmacro?

If I run this code
(defmacro foo
[expr]
(println "expr:" expr))
(foo '(1 2 3))
I will get the following print message
expr: (quote (1 2 3))
but I want to get the following message
expr: '(1 2 3)
I want to handle original reader macro string (before transformed) in defmacro. Maybe, I will access the string by reading a file which the macro is used and parse it, but not so cool. Please let me know if you know a much better way to do the mentioned above.
Thanks in advance.
according to http://www.clojure.org/reference/reader
The behavior of the reader is driven by a combination of built-in constructs and an extension system called the read table.
and
The read table is currently not accessible to user programs.
so I'd say no. Which is consistent with what I remember from various clojure books.
One thing which seemingly can be preserved are reader-conditionals
(read-string
{:read-cond :preserve}
"[1 2 #?#(:clj [3 4] :cljs [5 6])]")
;; [1 2 #?#(:clj [3 4] :cljs [5 6])]

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

Scheme cons won't take two number arguments

I've seen many instances of cons taking two numbers as arguments, and I've been told to pass pass two numbers as arguments into cons in a lab, but whenever I do I get the following error:
> (cons 1 2)
cons: second argument must be a list, but received 1 and 2
If I do the following, I get the same error:
> (cons '1 '2)
cons: second argument must be a list, but received 1 and 2
I'm very new to Scheme, and I don't understand why this is happening.
That's because of the teaching language in use, it's probable that you're working with a student language with certain limitations. To solve the problem, make sure that this line is at the beginning of the file:
#lang racket
And choose the option "Determine language from source" in the bottom-left corner of DrRacket's window. Now this should work as expected:
(cons 1 2)
=> '(1 . 2)

Step-by-step example of a lazy-seq

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)