Write a LISP code that prints the even number from a list of 10 values - lisp

I am a beginner in lisp language so kindly tell me how to make a lisp of 10 variables and print even numbers from list I try this
(write (list 1 2 3 4 5 6 7 8 9 10))
terpri

Related

Mapping a unary function to a list - why is `each` not always required?

I am going through a tutorial on q.
Here is the initial setup.
buys:2 1 4 3 5 4
sells:2 4 3 2
I then defined the following function
f:{x & (sums buys)}
I tried to use it in
f (sums sells)
But I get the following error
'length
[1] f:{x & (sums buys)}
I note that if instead I do this, it works
q)f each (sums sells)
2 2 2 2 2 2
2 3 6 6 6 6
2 3 7 9 9 9
2 3 7 10 11 11
However, the following unary function outputting a list has no problem being applied
q){(x*x;x)} (1 2 3)
1 4 9
1 2 3
The type signature of the 2 functions seems to be the same to me. Why does one get evaluated while the other raises an error?
Your 'length error is due to trying to perform the & operation with two lists which are not the same length.
We can inspect the variables in the debug session that q will drop you into if you run this on the REPL.
q)f (sums sells)
'length
[1] f:{x & (sums buys)}
^
q))x
2 6 9 11
q))buys
2 1 4 3 5 4
& requires both lists to be the same length, or one input to be a list and one to be an atom, so it can do item by item comparison.
With that in mind we can see that
f each (sums sells)
Works because now we are stepping through our sums sells list and comparing one item at a time to our buys list.
For the case of your unary function, this works because the * operation is being applied to two integer lists of the same length, so it will always work.
You can read more on functions (and the underlying vector ideas) here https://code.kx.com/q4m3/6_Functions/
It's detailed somewhat in q for mortals here: https://code.kx.com/q4m3/4_Operators/#45-greater-and-lesser
Specifically "Being atomic they operate item-wise on lists...."
Generally, kdb/q wants to do list operations itemwise, e.g.
q)1 2+2 3
3 5
If you give it lists of different length:
q)1 2+1 2 3
'length
[0] 1 2+1 2 3
^
it doesn't know if you want to do
q)1 2+\:1 2 3
2 3 4
3 4 5
or
q)1 2+/:1 2 3
2 3
3 4
4 5
Same applies to & in your case.
Your other function is not comparable because the only itemwise operation it does x*x is on lists of the same length. So that's ok.
Another way to hack it compare only first four(or whatever number) values
f:{x & (sums buys[till count sells])}

How do I set random numbers that fall in a range in kdb+?

In Kdb+, how do I use the "roll" function to make the random numbers generated fall within a range that doesn't start with 0? For example what if I wanted the range to be within 2-10 instead of 0-10?
What do I have to add to the code to make it fall into a range instead of the default 0-x? I have tried and looked for every method but can't seem to find one.
You could also just roll from 0-8 then add two. This doesn't require a list to be pre-generated
q)2+5?9
10 2 7 10 7
Assuming you want 2-10 inclusive
// quick and simple method
q)10?2+til 8
6 2 4 3 4 3 4 5 4 7
// or function (x)=num to be dealt, (y) start range, (z) end range
q)f:{x?y+til 1+z-y}
q)f[10;10;20]
12 17 10 11 19 12 11 18 18 11
If you supply a list in the right hand argument then you will get a random value from that list. To roll for a random range from 2-10 you can use til to generate the range:
q)2+til 9
2 3 4 5 6 7 8 9 10
q)1?2+til 9
,6
You can even supply a general list to randomly draw from:
q)3?(`abc;2 3f;10;20;30;"text")
2 3f
`abc
"text"
Simple math function for random number generator is:
(rand() mod (1+max- min)) + min
q) f:{x+rand[0] mod 1+y-x}
q) f[5;10]
q) 7
Update: I failed to notice that you wanted to generate couple of random numbers in the range. You could easily modify above function for that:
q) f:{x+(z?0) mod 1+y-x}
q) f[2;10;4]
q) 6 4 7 2

Genetic Algorithm for Flow shop Scheduling

I need help in Matlab: I need to find out how to Crossover any two sequences for genetic alghorithm in FlowShop, e.g.
1st sequence = 1 5 4 7 3 2 9 8 10 6
2nd sequence = 7 8 9 10 5 4 2 1 3 6
after crossover, the off-springs should be
offspring 1 = 1 5 4 7 3 2 8 9 10 6
offspring 2 = 7 8 9 10 1 5 4 3 2 6
Crossover should be such that each number doesn't repeat itself in the offspring sequence. Can anyone tell me how to do this?
There are a number of existing crossovers defined for permutation encodings. Among them the following would be useful for you:
Cyclic Crossover
Partially Matched Crossover
Uniform-like Crossover
Position-based Crossover
These crossovers aim to preserve the position of the job in the permutation. You can find implementations in C# in the PermutationEncoding plugin of HeuristicLab. Browse the source files and you can also find references to scientific articles that describe these crossovers.

Sorting different numbers into equal stacks

Hello I'm sort of new to matlab but I am working on a program that will take a bunch of numbers (prices) and sort them into groups that have near the same or near equivalent value (equal cost). However I'm not sure how to program that... does this already exist? If not how should I go about sorting these items.
Ex. sort 3 4 4 4 5 6 7 into 4 piles. 33/4 = 8.25 = Avg
Piles are 7 (4+4) (3+6) (4+5) or 7 8 9 9
Obviously these would also have two decimal places behind them (3.XX) any ideas?

How to find out if numbers are different in pascal?

i can't solve one pascal problem, first i have to enter N which is the number or mesurments, next i enter those mesurments (if N is 5 i enter 5 mesurments for exmaple 3 4 5 6 7). After that i enter Q which is the number of times i check the number of different numbers in a specific part of the row. For example you have input
5 (number of mesurments)
1 2 3 3 4 (mesurments)
3 (number of checkings)
1 3
3 4
1 5 (left number is the starting number from the row, if it's 1 you start from 1, if it's 4 you start from 3 because 3 is the fourth mesurment and the right number is the last number if it's 5 it would be 4 because 4 is the fifth in the row)
And for output you would get
3
1
4 (for the first checking you have number from 1 to 3 (1 2 3) and you have 3 different numbers, second mesurment from 3 to 4 (3 3) and it's 1 different number and last from 1 to 5 there are 4 different numbers)
Hope you understod me, and thanks if you can solve it, i am in highschool now and i am preparing my self for something more complicating :) thanks in advance
The inequality operator of Pascal is <>