manipulating lists in common lisp [closed] - functional-programming

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how does one create a list from a list,what function can i really use i was thinking of using mapcar or maplist with cons together but im not getting any fruitful results,lets say i have a list (a b) then i want a function that will create a list containing the same elements but they should be inform of lists like this ((a) (b)) ,any ideas on how i can solve this problem?? is there a function a use to it?
if i have a list(a b)
the result should be ((a)(b))
thanks guys

What you want to do is this:
(defun listify(ls)
(mapcar (lambda (elem) (list elem)) ls))
EDIT
Which is the same as (Thanks to #RainerJoswig):
(defun listify(ls)
(mapcar #'list ls))
And if you do:
(listify (list 1 2 3))
or
(listify '(1 2 3))
The output will be:
((1) (2) (3))

Related

Common Lisp: Why does this create circular list: (setf (car ) (append ))? [duplicate]

This question already has an answer here:
I want to make circular list with common lisp [duplicate]
(1 answer)
Closed 4 years ago.
I am trying to understand why this produces what seems to be circular list.
* (progn
(setf (car *x*) (append '(3) *x*))
2)
2 ;; No "apparent issue setting the value. Hence it is related to printing `*x*`
*x* ;; infinite loop, perhaps due to the structure of *x*??
Why is it a circular list? I would expect that it should not be a circular list
What is different between this question and the "duplicate" question:
In this question, I believe *x* should not be a circular list. In the duplicate answer chain, it is shown how to create a circular list, and neither of the example uses the result of append in the setf.
Alright, I found the answer:
My confusion arises from misunderstanding the spec where they say that append returns a new list.
Evidently a new list does not mean that each and every member of it is new (does not mean a copy is returned). The last argument of append is actually shared...
It's not the reader, but the printer that is in an infinite loop.
Most implementations have a variable to limit the top level printer,
see *PRINT-LEVEL*, *PRINT-LENGTH*

Work with string in common lisp [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code works fine:
(let* ((str (read-line)))
(write (char str 1)))
But if I add some, it breaks:
(let* ((str (read-line))
(str-len (length str))))
(write (char str 1)))
*** - EVAL: variable STR has no value
Why?
let and let* introduce bindings in a set of forms with the following syntax:
(let bindings
declarations
forms)
So the bindings have the value specified only inside the forms.
You have written:
(let* ((str (read-line))
(str-len (length str))))
(write (char str 1)))
If we align it to make it more understandable, it is simply:
(let* ((str (read-line))
(str-len (length str))))
(write (char str 1)))
^
| invalid parenthesis
So, you can discover that you have the forms part of the let* empty, which is legal in Common Lisp, and its value is nil.
You can also discover that the write form is outside the let*, and for this reason, the variables str and str-len are unknown, since they are outside of the global let form.
As specified in a comment, if you use an editor which knows the syntax of Common Lisp (there are several available), you can find this kind of errors as soon as you type your code. For instance, in this case you would have noticed immediately the extra parenthesis, which is highlighted clearly by such an editor. Moreover, the editor would have aligned correctly the forms, with write aligned under let*.

I dont understand recursive functions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Write a recursive function that computes the value of the recursively defined function
F(n)= -F(n-2), F(0)=1 and F(1) = -2. Iv been staring at this for hours i don't understand. Thanks for help.
Like any programming problem, you can first express the solution using pseudo code, and then move on to implementing it in your language of choice, e.g.
FUNCTION F(n)
IF n == 0 -- recursion terminates when n = 0, with result 0
RETURN 0
ELSE IF n == 1 -- recursion can also terminate when n = 1, with result -2
RETURN -2
ELSE -- otherwise recursion contiunes with F(n - 2), F(n - 4), ...
RETURN -F(n - 2) -- until one of the terminating conditions is reached
END
From here it should be quite straightforward to implement this function in C++ or whatever language you choose. Be sure to also implement a "test harness", i.e. a function which calls F with a range of different input values and then prints the result, so that you can verify whether the function is behaving correctly and debug it if necessary.

Get only decimal part in float number in SQL Server [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have following number in SQL Server 2008 : 5.4564556
I just want to get only 4564556 from 5.4564556
it would be better if i can get only 4 among 4564556 that is first digit of fractional part..
How can i do it?
Thanks
You can try in this way :
SELECT (5.4564556 % 1)
You can also use FLOOR():
SELECT Value - FLOOR(Value)
In your case:
5.4564556 - 5 = 0.4564556
in case of negative value you can use ABS
SELECT ABS(Value) - ABS(FLOOR(Value))
To get all the numbers behind the '.', you can (ab)use PARSENAME
select PARSENAME(5.4564556,1)
returns 4564556
Refer also to SQL - How do I get only the numbers after the decimal?
you can get first four decimal parts by :
select SUBSTRING (PARSENAME(5.4564556,1), 1, 4)
Try this
select cast(5.4564556 % 1 * 10 as int)

2D matrix of QStrings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to make an application with which you can reserve ticket for your travel. In fact, I'm designing the system for an airline. When I want to create a database (a 2D matrix that saves the number of seats in flights), it gives me errors.
The number of flights set in different place and the number is changing this is my code:
QString** matrix = new QString*[numberofFlights];
for (int i = 0; i < numberofFlight; i++)
{
matrix[i] = new QString[numberofSeats];
}
What class in Qt should i use?
A must-read: Qt container classes.
You could use QVectors or QLists or another container class. For example, to build a vector of vectors:
QVector< QVector<QString> > matrix(numberOfFlights);
for (int i=0; i<numberOfFlights; i++)
matrix[i].fill("", numberOfSeats);
This will create numberOfFlights vectors, that each contain numberOfSeats empty strings.
To set a specific seat:
matrix[flight][seat] = "whatever";
You can iterate over the vectors with the usual Qt foreach, or iterators, or plain for.

Resources