description:
i have a list(named large-number-list) which contains many number in it,and i want get the sum of these number.
now i divide the list into three element as one group to calculate(this group will be calculated in a action of agent), and put the sum of these three element into a vector(named result).
at last,i accumulate the element in the vector together.
code as following:
;use agent to calculate many number
(def result (agent []))
(def large-number-list [1 2 3 4 5 6 7 8 9 10 11 12]);assume that large-number-list contains many number in it
(defn doin3 [col do-fn]
(let [[x1 x2 x3 & rest-elem] col
rest-len (count rest-elem)]
(println "x1 x2 x3" x1 x2 x3)
(println "rest-len is " rest-len)
(do-fn x1 x2 x3)
(when (> rest-len 0) (doin3 rest-elem do-fn))))
;assume that the calculate is a time-consumed operation
(defn calculate [v x1 x2 x3]
(conj v (+ x1 x2 x3)))
(doin3 large-number-list #(send result calculate %1 %2 %3))
(println "before await")
(await result)
(println "after await")
(println #result)
(def total (apply + result))
(println "total is:" total)
(shutdown-agents)
expected output:
x1 x2 x3 1 2 3
rest-len is 9
x1 x2 x3 4 5 6
rest-len is 6
x1 x2 x3 7 8 9
rest-len is 3
x1 x2 x3 10 11 12
rest-len is 0
before await
after await
total is: 78
actual output:
x1 x2 x3 1 2 3
rest-len is 9
x1 x2 x3 4 5 6
rest-len is 6
x1 x2 x3 7 8 9
rest-len is 3
x1 x2 x3 10 11 12
rest-len is 0
before await
question:
the code run to "before await" and block,i guess that the action in agent is not finished,but why?
please let me know what is wrong with my code?
I think the problem is with this line:
(def total (apply + result))
It should be:
(def total (apply + #result))
It wasn't actually blocking, it was throwing an exception.
One more note: you should consider using recur in doin3, instead of direct call, as it's in a tail position already.
Related
Objective function => x1 - 2x2
Subject to =>
x2 <= 5
x1 - x2 >= 2
x1 ,x2, x3 >= 0
Maximize?
convert to standard form :
Maximize -> -x1 + 2x2
Subject to ->
x2 <= 5
-x1 + x2 <= -2
convert to slack form :
Z = -x1 + 2x2
x3 = 5 - x2
x4 = -2 +x1 -x2
Basic solution (0,0,5,-2)
Can I found optimal solution in here? If not why?
I have the following data frame:
df1 <- data.frame(x1=c(1,2,3,4), x2=c(10,20,30,40), x3=c(100,200,300,400))
And I want to generate al the possible data frames that can be created from combining d1$x1, df1$x2 and df1$x3 in different orders so 4^3 different dataframes, e.g:
x1 x2 x3
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400`
x1 x2 x3
1 1 40 400
2 2 30 300
3 3 20 200
4 4 10 100
and so on. For each of them I want to compute the following function:
my.function <- function(x1, x2, x3) {
sum(0.3*x1^2+0.3*x2^2+0.4*x3)/nrow(x1)
}
I did this, but it's clearly wrong:
res1 <- rep(NA, nrow(df1)^3)
for(i in 1:nrow(df1)){
for(j in 1:nrow(df1)){
for(k in 1:nrow(df1)){
x1.1 <- as.vector(c(df1[-i, 1], df1[i, 1]))
x2.1 <- as.vector(c(df1[-k, 2], df1[k, 2]))
x3.1 <- as.vector(c(df1[-j, 3], df1[j, 3]))
res1[nrow(df1)^2*(i-1) + nrow(df1)*(j-1)+k] <- m.function(x1.1, x2.1, x3.1)
}
}
}
I tried to find a similar problem of mine without much luck, could you please help me?
Thank you so much!!!
Does exist a function in LISP for making a sequence of integers like (0 1 2 3)?
I found make-sequence, but I didn't find out how to make a sequence of integers.
I tried make-list and nothing.
I know that in Scheme exists (build-list 5 (lambda (x) x)). I tried to change the build-list with make-list, but it didn't work.
Some ideas? Thanks
Edit: I need something like make-list 5 ==> (0 1 2 3 4)
Simply done with loop:
(loop :for n :below 10 :collect n)
; ==> (0 1 2 3 4 5 6 7 8 9)
The Alexandria library, which is intended to work on any conforming implementation of Common Lisp, defines iota:
(iota 5)
=> (0 1 2 3 4)
You can also customize start and step:
(iota 3 :start 1 :step 1.0)
=> (1.0 2.0 3.0)
But often you do not need to actually produce the list, you just want to iterate over the given range. That's why there is also map-iota:
(map-iota #'print 3 :start 1 :step 1.0)
=> 3
In such cases you can of course use LOOP:
(loop for i from 1.0 below 22 by 1.5 do (print i))
Instead of do, you can also collect and obtain a list; this is a bit more verbose than iota, but easier to customize.
Lets see if can still write mac lisp of the top of my head:
(defun foo (num acc)
(if (eq num 0)
acc
(foo (- num 1) (cons num acc))))
(foo 5 nil)
should be
(1 2 3 4 5)
Say I have a lazy sequence like the following:
(def s (iterate inc 1))
(take 10 s)
=> (1 2 3 4 5 6 7 8 9 10)
Now, I want to generate a sequence of cumulative sum of s like the following:
=> (1 3 6 10 15 ...)
How can I do this?
What I tried is to use atom and accumulate the sum to it(mutating) Is this the only way to generate cumulative sequence or is there a better way to do this?
NOTE: the above cumulative sum is only an example. The source sequence can be other sequence. So I can't use formula: s(n) = n(n+1)/2
(take 10 (reductions + s))
=> (1 3 6 10 15 21 28 36 45 55)
I've been tasked with writing a function that generates a table given n operators. The truth table must be in a list and each row of the table must be in separate lists (inside the main list).
I know the solution involves recursion but I just can't seem to think it through.
Can someone help me out? This is only a small part of the assignment.
Easiest way I can think of off the top of my head is to simply convert 2^n to binary and count down, then convert the output to a list.
ie for n=3:
Truth table:
a b c
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
2^3 = 8, 8 in binary = 1000, start from 1000-1 = 111 and work your way down to 0, record outputs, and voila!
If hkf's interpretation of your question is right, this should work in Racket:
#lang racket
(define (generate-table n)
(if (zero? n)
'(())
(for*/list ((y (in-list (generate-table (sub1 n))))
(x (in-list '(0 1))))
(cons x y))))
Use it like this:
(generate-table 3)
> ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1))
Let's assume that all N operators are binary functions, like AND and OR.
;; Common Lisp
(defun truth-tables (ops)
(loop for op in ops
collecting
(loop for args in '((nil nil) (nil t) (t nil) (t t))
collecting (eval `(,op ,#args)))))
(truth-tables '(and or xor)) -> ((NIL NIL NIL T) (NIL T T T) (NIL T T NIL))
This gives you an idea. Well, here I don't have "each row of the truth table" as a sublist; I have the columns for the AND, OR and XOR truth tables, respectively. The input variable combinations are left implicit: you know that the third entry of every one corresponds to (<op> t nil). Your description of the problem is not very clear.
As you can also see, I cheated by using the Lisp operators through generated code which is dynamically evaluated.