Rounding to the nearest digit in list in Clojure - clojure

I am trying to get the rounded off version of a list of numerics in Clojure.
For e.g. (Math/round 10.5) gives me 11 as the answer which is fine. However, I try a similar approach using 'apply' - (apply Math/round (list 1.2 2.1 3.4)) which gives me an error. I was expecting (1 2 3) as the output.
How to get a rounded off output from a list of numbers?

Use map. Note that you'll have to wrap the call to Math/round in an anonymous function: see this question for the reason why.
(map #(Math/round %) (list 1.1 2.2 3.9)) ; '(1 2 4)
The problem with your approach (which was actually not a bad guess) is that Java's Math/round isn't variadic: it just takes a single argument. Calling apply that way is the same as saying
(Math/round 1.2 2.1 3.4)
Which will fail to compile.

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.

Scheme eval simple circumvention

I am currently learning scheme as part of a course I am taking. The way it has been taught to me is with generous use of the eval expression, and most of the provided examples I am testing do not work as described. The more I read, it becomes obvious that I should not be using eval so freely. I have seen some circumvention methods in javaScript but I am looking for something specific to Scheme.
This link provides some info about when eval may necessary. That doesn't really help me figure out a good circumvention method.
This link has more good info but doesn't give any way to avoid eval in my situation.
This link came closest to answering my question, and helped me to understand why eval does not necessarily work as expected, but I am still unclear on what a good replacement for the eval expression would be.
What I am looking for is the standard way to do this :
(define (add a_list)
    (cond
        ((null? a_list) 0)
        (eval(cons '+ a_list)))
)
(add '(3 4 8 12 30))
without eval so that it returns 57. Currently it returns (+ 3 4 8 12 30).
This link seems to suggest that, say, (+ 3 4) would automatically evaluate to 7, but I am getting just the list printed back to me as an expression with no evaluation.
It will work fine written the following ways:
(define (add a_list)
    (cond
        ((null? a_list) 0)
        (eval(eval(cons '+ a_list))))
)
(add '(3 4 8 12 30))
returns 57 but one of the evals seems to do nothing. And :
(define (add a_list)
  (eval(cons '+ a_list))
)
(add '(3 4 8 12 30))
returns 57 but does not check for an empty list.
Any help removing it altogether (or only using it if absolutely necessary) would be greatly appreciated.
Thanks in advance!
For the examples mentioned, you don't need eval, a simple apply will work. And your suspicions are correct, we should avoid using eval most of the time - it's considered "evil".
(define (add a_list)
(apply + a_list))

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

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)