Sequence of prime numbers in range - math

I would like to write a function prime-seq to show a list between two numbers, using from and to.
Here is my code, I think it is if the numbers from the list are true then display them. But I have no idea how to write it, I am very new for this language.
(defn is-prime? [n]
(empty?
(filter #(= 0 (mod n %)) (range 2 n))))
(defn prime-seq [from to]
(drop from (take to is-prime?)))
the result should be:
(prime-seq 1 5)
=> (2 3 5)

You're very close:
(defn is-prime? [n]
(empty? (filter #(= 0 (mod n %)) (range 2 n))))
(defn prime-seq [from to]
(filter is-prime? (range from (inc to))))
(prime-seq 1 29)
=> (1 2 3 5 7 11 13 17 19 23 29)
This is using range generate a sequence of all numbers between from and to (inclusive), then filtering that list using your is-prime? predicate.
As for your is-prime? predicate, there are many approaches to determining primeness. As you comment (mod 1 1) => 0, so your predicate returns true however 1 isn't a prime number. You can simply add a special case for this in your predicate so that any number less than 2 returns false:
(defn is-prime? [n]
(if (< 1 n)
(empty? (filter #(= 0 (mod n %)) (range 2 n)))
false))
Or slightly more terse using and:
(defn is-prime? [n]
(and (< 1 n)
(not (some #(= 0 (mod n %)) (range 2 n)))))

In case people come here for a reasonably fast algorithm for finding prime numbers implemented in Clojure, here's an implementation of the Sieve of Eratosthenes in Clojure (using JVM arrays):
(defn find-primes
"Finds all prime numbers less than n, returns them sorted in a vector"
[n]
(if (< n 2)
[]
(let [^booleans sieve (boolean-array n false)
s (-> n Math/sqrt Math/floor int)]
(loop [p 2]
(if (> p s)
(into []
(remove #(aget sieve %))
(range 2 n))
(do
(when-not (aget sieve p)
(loop [i (* 2 p)]
(when (< i n)
(aset sieve i true)
(recur (+ i p)))))
(recur (inc p))))))))
Example:
(find-primes 100)
=> [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97]
And some benchmarking:
(require '[criterium.core :as bench])
(bench/bench
(find-primes 100000))
;Evaluation count : 17940 in 60 samples of 299 calls.
; Execution time mean : 3.370834 ms
; Execution time std-deviation : 217.730604 µs
; Execution time lower quantile : 3.040426 ms ( 2.5%)
; Execution time upper quantile : 3.792958 ms (97.5%)
; Overhead used : 1.755126 ns

Related

Scheme - List of Fibonacci numbers up to certain value

I am trying to write a function that creates a list of the fibonacci sequence but stops when a certain value is found in the list, then returns that list (I hope that makes sense).
So for example if I give it fib-list(55), the function should return:
(1 1 2 3 5 8 13 21 34 55)
So it's not the 55th fibonacci number I want, its the list UP TO the value 55.
The code I have for returning the list so far looks like this:
; Create a list of the fibonacci sequence up to n.
(define (fib-list n)
; n = n, f2 = 1, f1 = 1, fs = a list.
(let loop ((n n) (f2 1) (f1 1) (fs (list)))
(cond
; If n = 0, return reversed list.
((zero? n) (reverse fs))
; Check if n is in list. If so, return list.
((equal? n (car fs)) fs)
;Else, find the next fibonacci number and add it to the list.
(else (loop (- n 1) f1 (+ f2 f1) (cons f2 fs))))))
(display (fib-list 55))
My main problem is finding if an element is in the list, because at the moment I just get an error on the line where I am trying to write the ((equal? statement.
The error says:
mcar: contract violation
expected: mpair?
given: '()
I am still very VERY new to Scheme, so my understanding of the language as a whole isn't great. So please be gentle when telling me why my code sucks/doesn't make sense.
(list) creates an empty list, and on the first iteration you get to (car fs), which tries to apply car to an empty list, and that's an error.
Your code seems a bit confused about the nature of n.
Your description says that it's the largest number you want, but you're recursing like you want the n:th Fibonacci number - terminating on (zero? n) and recursing on (- n 1).
When you're recursing you're still looking for numbers up to the same limit.
Thus, you should not decrement your limit and terminate on zero, you should leave the limit alone and terminate when you reach larger numbers.
Here's how I would write it:
The initial list is (1 1)
At each step:
Compute the next fibonacci number
If this is greater than the limit, reverse the accumulator list and return it
Otherwise, cons it to the accumulator and recurse with the "new" last two fibonacci number.
In code:
(define (fib-list n)
(let loop ((f2 1) (f1 1) (fs '(1 1)))
(let ((next (+ f1 f2)))
(if (> next n)
(reverse fs)
(loop f1 next (cons next fs))))))
Here's another way you can do it using continuation-passing style. By adding a continuation parameter to our loop, we effectively create our own return mechanism. One unique property of this implementation is the output list is built in forward order and does not need to be reversed when n reaches zero.
(define (fib-list n)
(let loop ((n n) (a 0) (b 1) (return identity))
(if (zero? n)
(return empty)
(loop (sub1 n)
b
(+ a b)
(lambda (rest) (return (cons a rest)))))))
(fib-list 10)
;; '(0 1 1 2 3 5 8 13 21 34)
Reading your question a little closer, in fib-list(N) you need N to be the stopping condition for your loop, not the Nth term in the list. This is actually easier to implement as there's no need to count the number of terms generated.
(define (fib-list max)
(let loop ((a 0) (b 1) (return identity))
(if (> a max)
(return empty)
(loop b
(+ a b)
(lambda (rest) (return (cons a rest)))))))
(fib-list 55)
;; '(0 1 1 2 3 5 8 13 21 34 55)
(fib-list 1000)
;; '(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987)
What's going wrong with the car function?
The car function takes the first element of a list, but if the list is empty it doesn't have a first element. The fs list starts out as empty. When you try to take the first element of an empty list you get this error message:
> (car (list))
mcar: contract violation
expected: mpair?
given: ()
If the list isn't empty, then it has a first element, and it's fine:
> (car (list 4 5 6))
4
Following what you meant in the comment
However, your comment "Check if n is in list" leads me to believe that (equal? n (car fs)) is not what you want anyway. The function for determining whether an element is in a list is called member.
#!r6rs
(import (rnrs base)
(rnrs lists))
> (if (member 4 (list 1 2 4 8))
"it's in the list"
"go fish")
"it's in the list"
> (if (member 5 (list 1 2 4 8))
"it's in the list"
"go fish")
"go fish"
So with that (equal? n (car fs)) test replaced with (member n fs), your code looks like:
; Create a list of the fibonacci sequence up to n.
(define (fib-list n)
; n = n, f2 = 1, f1 = 1, fs = a list.
(let loop ((n n) (f2 1) (f1 1) (fs (list)))
(cond
; If n = 0, return reversed list.
((zero? n) (reverse fs))
; Check if n is in list. If so, return list.
((member n fs) fs)
;Else, find the next fibonacci number and add it to the list.
(else (loop (- n 1) f1 (+ f2 f1) (cons f2 fs))))))
> (fib-list 55)
(10946 6765 4181 2584 1597 987 610 377 233 144 89 55 34 21 13 8 5 3 2 1 1)
This is not the answer you wanted though; you wanted (1 1 2 3 5 8 13 21 34 55).
Why is the list going past 55?
One of the problems is that the n is shadowed, in the same way that in this expression:
> (let ([n 5])
(let ([n 10])
n))
10
The n in the body refers to 10 instead of 5.
The result is going past 55 because inside the loop n is shadowed and has become a different number. I'm guessing in your comment about "check if n is in list", you meant "check if the original n is in list". To do that you have to rename one of the ns:
> (let ([orig-n 5])
(let ([n 10])
orig-n))
5
In the context of your code:
; Create a list of the fibonacci sequence up to n.
(define (fib-list orig-n)
; n = n, f2 = 1, f1 = 1, fs = a list.
(let loop ((n orig-n) (f2 1) (f1 1) (fs (list)))
(cond
; If n = 0, return reversed list.
((zero? n) (reverse fs))
; Check if orig-n is in list. If so, return list.
((member orig-n fs) fs)
;Else, find the next fibonacci number and add it to the list.
(else (loop (- n 1) f1 (+ f2 f1) (cons f2 fs))))))
> (fib-list 55)
(55 34 21 13 8 5 3 2 1 1)
Reversing
This is closer, but it's reversed. You have two base cases, the (zero? n) case and the (member orig-n fs) case. In one of those it's reversed and in one of them it's not. Changing them both to call reverse fixes it:
; Create a list of the fibonacci sequence up to n.
(define (fib-list orig-n)
; n = n, f2 = 1, f1 = 1, fs = a list.
(let loop ((n orig-n) (f2 1) (f1 1) (fs (list)))
(cond
; If n = 0, return reversed list.
((zero? n) (reverse fs))
; Check if orig-n is in list. If so, return reversed list.
((member orig-n fs) (reverse fs))
;Else, find the next fibonacci number and add it to the list.
(else (loop (- n 1) f1 (+ f2 f1) (cons f2 fs))))))
> (fib-list 55)
(1 1 2 3 5 8 13 21 34 55)
Small numbers
This is correct on large Fibonacci numbers like 55, but it still does something weird on small numbers:
> (fib-list 2)
(1 1)
> (fib-list 3)
(1 1 2)
If you only want it to stop when it gets to orig-n, then maybe the decreasing n argument is not needed, and is actually making it stop too early. Removing it (and removing the zero check for it) makes the member check the only stopping case.
This is dangerous, because it could go into an infinite loop if you give it a non-Fibonacci number as input. However, it fixes the small-number examples:
; Create a list of the fibonacci sequence up to n.
; The `orig-n` MUST be a fibonacci number to begin with,
; otherwise this loops forever.
(define (fib-list orig-n)
; f2 = 1, f1 = 1, fs = a list.
(let loop ((f2 1) (f1 1) (fs (list)))
(cond
; Check if orig-n is in list. If so, return reversed list.
((member orig-n fs) (reverse fs))
;Else, find the next fibonacci number and add it to the list.
(else (loop f1 (+ f2 f1) (cons f2 fs))))))
> (fib-list 55)
(1 1 2 3 5 8 13 21 34 55)
> (fib-list 2)
(1 1 2)
> (fib-list 3)
(1 1 2 3)
And finally, consider what happens vs. what should happen if you give it a number like 56.
> (fib-list 56)
;infinite loop
This is a design decision that you have not specified in your question (yet), but there are ways of solving it either way.
Update: orig-n or greater
I should have specified that I need to check if there is a number that is greater than OR equal to orig-n. Can I still use the member function to check for this or will I need to use something different?
You will have to use something different. Just above member in the documentation is the memp function (you could also use exists in this case). The mem is short for member, and the p is short for "predicate". It determines whether any member of the list matches a certain predicate.
> (if (memp positive? (list -4 -2 -3 5 -1))
"one of them is positive"
"go fish")
"one of them is positive"
> (if (memp positive? (list -4 -2 -3 -5 -1))
"one of them is positive"
"go fish")
"go fish"
> (define (five-or-greater? n)
(>= n 5))
> (if (memp five-or-greater? (list -4 -2 -3 6 -1))
"one of them is equal to 5 or greater"
"go fish")
"one of them is equal to 5 or greater"
> (if (memp five-or-greater? (list -4 -2 -3 4 -1))
"one of them is equal to 5 or greater"
"go fish")
"go fish"
To use it for "orig-n or greater", you would have to define a function like:
(define (orig-n-or-greater? n)
(>= n orig-n))
As a local function inside your main function, so that it can refer to orig-n. Then you can use it like (memp orig-n-or-greater? fs).
; Create a list of the fibonacci sequence up to n.
(define (fib-list orig-n)
(define (orig-n-or-greater? n)
(>= n orig-n))
; f2 = 1, f1 = 1, fs = a list.
(let loop ((f2 1) (f1 1) (fs (list)))
(cond
; Check if orig-n or greater is in list. If so, return reversed list.
((memp orig-n-or-greater? fs) (reverse fs))
;Else, find the next fibonacci number and add it to the list.
(else (loop f1 (+ f2 f1) (cons f2 fs))))))
> (fib-list 3)
(1 1 2 3)
> (fib-list 55)
(1 1 2 3 5 8 13 21 34 55)
> (fib-list 56)
(1 1 2 3 5 8 13 21 34 55 89)

Representing an amount of money with specific bills

I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1)) should return '(4 1 1 0 0).
I tried it this way but this doesn't work :/ I think I haven't fully understood what you can / can't do with set! in Racket, to be honest.
(define (calc n xs)
(cond ((null? xs) (list))
((not (pair? xs))
(define y n)
(begin (set! n (- n (* xs (floor (/ n xs)))))
(list (floor (/ y xs))) ))
(else (append (calc n (car xs))
(calc n (cdr xs))))))
Your procedure does too much and you use mutation which is uneccesary. If you split the problem up.
(define (calc-one-bill n bill)
...)
;; test
(calc-one-bill 450 100) ; ==> 4
(calc-one-bill 450 50) ; ==> 9
Then you can make:
(define (calc-new-n n bill amount)
...)
(calc-new-n 450 100 4) ; ==> 50
(calc-new-n 450 50 9) ; ==> 0
Then you can reduce your original implememntation like this:
(define (calc n bills)
(if (null? bills)
(if (zero? n)
'()
(error "The unit needs to be the last element in the bills list"))
(let* ((bill (car bills))
(amount (calc-one-bill n bill)))
(cons amount
(calc (calc-new-n n bill amount)
(cdr bills))))))
This will always choose the solution with fewest bills, just as your version seems to do. Both versions requires that the last element in the bill passed is the unit 1. For a more complex method, that works with (calc 406 (list 100 10 5 2)) and that potentially can find all combinations of solutions, see Will's answer.
This problem calls for some straightforward recursive non-deterministic programming.
We start with a given amount, and a given list of bill denominations, with unlimited amounts of each bill, apparently (otherwise, it'd be a different problem).
At each point in time, we can either use the biggest bill, or not.
If we use it, the total sum lessens by the bill's value.
If the total is 0, we've got our solution!
If the total is negative, it is invalid, so we should abandon this path.
The code here will follow another answer of mine, which finds out the total amount of solutions (which are more than one, for your example as well). We will just have to mind the solutions themselves as well, whereas the code mentioned above only counted them.
We can code this one as a recursive-backtracking procedure, calling a callback with each successfully found solution from inside the deepest level of recursion (tantamount to the most deeply nested loop in the nested loops structure created with recursion, which is the essence of recursive backtracking):
(define (change sum bills callback)
(let loop ([sum sum] [sol '()] [bills bills]) ; "sol" for "solution"
(cond
((zero? sum) (callback sol)) ; process a solution found
((< sum 0) #f)
((null? bills) #f)
(else
(apply
(lambda (b . bs) ; the "loop":
;; 1. ; either use the first
(loop (- sum b) (cons b sol) bills) ; denomination,
;; 2. ; or,
(loop sum sol bs)) ; after backtracking, don't!
bills)))))
It is to be called through e.g. one of
;; construct `the-callback` for `solve` and call
;; (solve ...params the-callback)
;; where `the-callback` is an exit continuation
(define (first-solution solve . params)
(call/cc (lambda (return)
(apply solve (append params ; use `return` as
(list return)))))) ; the callback
(define (n-solutions n solve . params) ; n assumed an integer
(let ([res '()]) ; n <= 0 gets ALL solutions
(call/cc (lambda (break)
(apply solve (append params
(list (lambda (sol)
(set! res (cons sol res))
(set! n (- n 1))
(cond ((zero? n) (break)))))))))
(reverse res)))
Testing,
> (first-solution change 406 (list 100 10 5 2))
'(2 2 2 100 100 100 100)
> (n-solutions 7 change 415 (list 100 10 5 2 1))
'((5 10 100 100 100 100)
(1 2 2 10 100 100 100 100)
(1 1 1 2 10 100 100 100 100)
(1 1 1 1 1 10 100 100 100 100)
(5 5 5 100 100 100 100)
(1 2 2 5 5 100 100 100 100)
(1 1 1 2 5 5 100 100 100 100))
Regarding how this code is structured, cf. How to generate all the permutations of elements in a list one at a time in Lisp? It creates nested loops with the solution being accessible in the innermost loop's body.
Regarding how to code up a non-deterministic algorithm (making all possible choices at once) in a proper functional way, see How to do a powerset in DrRacket? and How to find partitions of a list in Scheme.
I solved it this way now :)
(define (calc n xs)
(define (calcAssist n xs usedBills)
(cond ((null? xs) usedBills)
((pair? xs)
(calcAssist (- n (* (car xs) (floor (/ n (car xs)))))
(cdr xs)
(append usedBills
(list (floor (/ n (car xs)))))))
(else
(if ((= (- n (* xs (floor (/ n xs)))) 0))
(append usedBills (list (floor (/ n xs))))
(display "No solution")))))
(calcAssist n xs (list)))
Testing:
> (calc 415 (list 100 10 5 2 1))
'(4 1 1 0 0)
I think this is the first program I wrote when learning FORTRAN! Here is a version which makes no bones about using everything Racket has to offer (or, at least, everything I know about). As such it's probably a terrible homework solution, and it's certainly prettier than the FORTRAN I wrote in 1984.
Note that this version doesn't search, so it will get remainders even when it does not need to. It never gets a remainder if the lowest denomination is 1, of course.
(define/contract (denominations-of amount denominations)
;; split amount into units of denominations, returning the split
;; in descending order of denomination, and any remainder (if there is
;; no 1 denomination there will generally be a remainder).
(-> natural-number/c (listof (integer-in 1 #f))
(values (listof natural-number/c) natural-number/c))
(let handle-one-denomination ([current amount]
[remaining-denominations (sort denominations >)]
[so-far '()])
;; handle a single denomination: current is the balance,
;; remaining-denominations is the denominations left (descending order)
;; so-far is the list of amounts of each denomination we've accumulated
;; so far, which is in ascending order of denomination
(if (null? remaining-denominations)
;; we are done: return the reversed accumulator and anything left over
(values (reverse so-far) current)
(match-let ([(cons first-denomination rest-of-the-denominations)
remaining-denominations])
(if (> first-denomination current)
;; if the first denomination is more than the balance, just
;; accumulate a 0 for it and loop on the rest
(handle-one-denomination current rest-of-the-denominations
(cons 0 so-far))
;; otherwise work out how much of it we need and how much is left
(let-values ([(q r)
(quotient/remainder current first-denomination)])
;; and loop on the remainder accumulating the number of bills
;; we needed
(handle-one-denomination r rest-of-the-denominations
(cons q so-far))))))))

Calculating n-digit palindromes infinite loop

I am trying to go through SICP using Racket and Project Euler as reference for practice. This question is in regards to problem 4 (https://projecteuler.net/problem=4), hence:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
I am currently still on Chapter 1 (SICP) and loops have not been covered yet, as I see most solutions use. My code runs out of memory every time I try to execute (I have been stuck on this for hours - https://github.com/polila/Project-Euler/commits?author=polila&since=2018-03-19T04:00:00Z&until=2018-03-20T04:00:00Z) LOL.
What I am trying to do is use a recursive procedure by the process of linear iteration (at least that's what I think) - so in my mind it was something like this, where a and b are the max limit of n-digit:
(largest-palindrome a b max)
( 999 999 0)
( 999 998 0)
( 999 997 0)
( . . .)
( . . .)
( 999 99 x)
( 998 998 x)
( 998 997 x)
( 998 996 x)
( . . x)
( 100 100 x)
I realize that I wouldn't have to go all the way to the minimum range but even for small test cases such as 2-digit palindromes with the range of [90,99] it still doesn't execute. But here is my code, where am I going wrong??????????
#lang racket
(define (exp a b)
(define (iter product count)
(if (= count b)
product
(iter (* a product) (+ count 1))))
(if (= b 0)
1
(iter a 1)))
(define (palindrome? ab)
(define (length ab)
(define (iter n count)
(if (= n 0)
count
(iter (floor (/ n 10)) (+ count 1))))
(iter ab 0))
(define (check-symmetry ab left right)
(if (> left right)
(if (= (floor (/ (modulo ab (exp 10 left)) (exp 10 (- left 1))))
(floor (/ (modulo ab (exp 10 right)) (exp 10 (- right 1)))))
(check-symmetry ab (- left 1) (+ right 1))
#f)
#t))
(check-symmetry ab (length ab) 1))
(define (largest-palindrome a b max)
(if (> b 100)
(if (and (palindrome? (* a b)) (< max (* a b)))
(largest-palindrome a (- b 1) (* a b))
(largest-palindrome a (- b 1) max))
(largest-palindrome (- a 1) (- a 1) (* a b)))
(if (< a 100)
max
0))
You have the right general idea of looking at multiples starting at 999x999 all the way down to 100x100. You can improve efficiency on this method by terminating b whenever a x b is a palindrome, such that you don't test for palindromes in a x b-1 etc. since those products will be smaller in value than a x b.
What this looks like is:
999 x 999 -- not palindrome
999 x 998 -- not palindrome
|
995 x 583 -- palindrome (if current palindrome < this, save this value)
994 x 994 -- not palindrome (Notice the jump here instead of testing 995 x 582)
994 x 993 -- not palindrome
|
994 x 100 -- not palindrome
993 x 993 -- not palindrome
|
993 x 913 -- palindrome (if current < this, save this value)
992 x 992 -- not palindrome
|
100 x 100 -- not palindrome (hit base case and return current palindrome)
In your implementation of largest-palindrome, the first if-statement, ie. (if (> b 100) ...), is dead code. Moreover, the recursive steps in that if statement also have no base case(s), hence why you run out of memory every time as a result of the infinite recursion.
You can simplify much of your code. For example, consider the following palindrome? predicate that checks whether a number is equal to its digits reversed:
(define (palindrome? n)
(define (in-reverse n acc)
(if (< n 10)
(+ (* acc 10) n)
(in-reverse (quotient n 10)
(+ (* acc 10)
(remainder n 10)))))
(= n (in-reverse n 0)))
ie.
(palindrome? 1001)
=> #t
(palindrome? 1010)
=> #f
You can then implement largest-palindrome using this predicate:
;; largest-palindrome: Finds the largest palindrome that is a
;; multiple of two numbers, each of which has n digits.
(define (largest-palindrome n)
(define low (expt 10 (sub1 n))) ;; if n = 3, low = 100
(define high (sub1 (expt 10 n))) ;; if n = 3, high = 999
(define (largest a b mymax low high)
(define prod (* a b))
(cond
((< a low) mymax)
((or (< b low)
(< prod mymax))
(largest (sub1 a) (sub1 high) mymax low (sub1 high)))
((and (> prod mymax)
(palindrome? prod))
(largest (sub1 a) (sub1 high) prod b (sub1 high)))
(else
(largest a (sub1 b) mymax low high))))
(largest high high 0 low high))
this works for various values of n:
(largest-palindrome 3)
=> 906609
(largest-palindrome 5)
=> 9966006699
(largest-palindrome 7)
=> 99956644665999

Where can I place the print function inside a loop/recur in Clojure?

Getting started with Clojure and functional programming. Looking at the slow example of adding numbers in a range from the not-yet-published programming Clojure book third edition, chapter 10.
How to add a println to this loop so that I can see the values of sum as they change?
; performance demo only, don't write code like this
(defn sum-to [n]
(loop [i 1 sum 0]
(if (<= i n)
(recur (inc i) (+ i sum))
sum)))
The loop macro accepts multiple body expressions, so you can insert a side-effecting println expression right before your if expression:
(defn sum-to [n]
(loop [i 1 sum 0]
(println sum)
(if (<= i n)
(recur (inc i) (+ i sum)) sum)))
Here Clojure will use the result of the last expression in the body as the return value of the whole loop expression. Example:
(sum-to 5)
;; 0
;; 1
;; 3
;; 6
;; 10
;; 15
;;=> 15
Another handy tool which in my opinions solves your problem as well but in a cleaner manner instead of printing is trace. It's extremely useful when debugging.
(use 'clojure.tools.trace)
(defn sum-to
[n]
(loop [i 1 sum 0]
(if (<= i n)
(recur (inc i) (trace (+ i sum))) sum)))
(sum-to 10)
TRACE: 1
TRACE: 3
TRACE: 6
TRACE: 10
TRACE: 15
TRACE: 21
TRACE: 28
TRACE: 36
TRACE: 45
TRACE: 55
55
Not an answer to your question, but
(reductions + (range 6))
Gives you what you’re looking for.
A handy tool I often use for this purpose is the spyx macro from the Tupelo library.
It works like so:
(ns tst.demo.core
(:use demo.core tupelo.test)
(:require
[tupelo.core :as t] ))
(t/refer-tupelo)
(defn sum-to [n]
(loop [i 1 sum 0]
(if (<= i n)
(recur (inc i) (spyx (+ i sum)))
sum)))
(spyx (sum-to 5))
with result:
(+ i sum) => 1
(+ i sum) => 3
(+ i sum) => 6
(+ i sum) => 10
(+ i sum) => 15
(sum-to 5) => 15

Why I'm getting a list instead of a vector?

So, I'm trying to transform each element of a vector x,in this way: x[i]--> 1-(1/x[i])
(defn change[x]
(fn [i]
(assoc x i (- 1 (/ 1 (get x i))))
)
(range 0 (* (count x) 1))
)
I'm using assoc to replace each element of the vector, I'm supposed to get a vector with the changes, but instead I'm getting a list.
For example
user> (change [21 32 23 34])
(0 1 2 3)
But I should get a vector :v
The code for the function you provided doesn't use the local anonymous function, and can be refactored greatly.
This is your original function with comments.
(defn change[x]
;; start unused anonymous
(fn [i]
(assoc x i (- 1 (/ 1 (get x i)))))
;; end unused anonymous
;; start/end gen list of ints
(range 0 (* (count x) 1)))
This is probably what you mean
(defn change [coll]
(mapv #(- 1 (/ 1 %)) coll))
And this is the output
user> (change [21 32 23 34])
;=> [20/21 31/32 22/23 33/34]
What your code does
Your original code (reformatted)
(defn change [x]
(fn [i] (assoc x i (- 1 (/ 1 (get x i)))))
(range 0 (* (count x) 1)))
evaluates and discards a function value then
returns the range.
So you can omit the fn form and reduce it to
(defn change [x]
(range 0 (* (count x) 1)))
which in turn reduces to
(defn change [x]
(range (count x)))
So, for example,
(change [:whatever :I :choose :to :put :here])
;(0 1 2 3 4 5)

Resources