Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a homework and I'm not allowed to use loops or global variables.
Basically the function looks like
(defun pairs (1 4)
and makes a list like so (1 2 3 4) and finds all possible pairs, so it should return ((1 2) (1 3) (1 4) (2 3) (2 4) (3 4)). All the code I've tried don't give me all the pairings, usually resulting in just getting pairs from the start to the end, like (1 2) (2 3) (3 4). I'm also not sure if the base case should be when (= m n) or (= (1- m) n).
It's unlikely that you can use mapcon in your solution, so I don't feel like the following code is giving you a homework answer. If you read up on what mapcon does, though, and understand how to implement it, you can use the following as a guide to a solution.
(defun pairs (list)
(mapcon (lambda (tail)
(mapcar (lambda (y)
(list (first tail) y))
(rest tail)))
list))
CL-USER> (pairs '(1 2 3 4))
;=> ((1 2) (1 3) (1 4) (2 3) (2 4) (3 4))
The idea here is that if you'd want to recurse on the tails of your original list. That is, consider (1 2 3 4) and generate some pairs from that, then consider (2 3 4) and generate some pairs from that, then (3 4), and then (4) and generate (an empty set of) some pairs from that:
(1 2 3 4) → [1, (2 3 4)] ↦ ((1 2) (1 3) (1 4))
(2 3 4) → [2, (3 4)] ↦ ((2 3) (2 4))
(3 4) → [3, (4)] ↦ ((3 4))
(4) → [4, ()] ↦ ()
Then you just need to put ((1 2) (1 3) (1 4)), ((2 3) (2 4)), ((3 4)), and () together to get ((1 2) (1 3) (1 4) (2 3) (2 4) (3 4)).
Related
How would I write a function in Dr. Racket which consumes a list of list of integers and produces a new list of list of integers with a transformation (sqr) applied to each element.
Note: There is an equal number of elements in each list.
Here are two examples to show what I am saying:
Example 1:
(list (list 1 2) (list 3 -2))
Should produce:
(list (list 1 4) (list 9 4))
Example 2:
(list (list 3 4 5) (list 2 1 3) (list 2 3 7) (list 0 -3 2))
Should produce:
(list (list 9 16 25) (list 4 1 9) (list 4 9 49) (list 0 9 4))
Here is my code so far:
(define (transform-to-sqr b)
(map sqr b))
If I type in:
(transform-to-sqr (list (list 1 2) (list 3 -2)))
I get an error message saying:
sqr: expected a number; given (list 1 2).
Why is this happening. Can someone please tell me what I am doing wrong.
Thanks for your help.
The map higher-order procedure applies a procedure over the elements of a list, but in this case the elements are lists themselves. To operate on the nested elements, we need to nest calls to map:
(define (transform-to-sqr matrix)
(map (λ (row) (map sqr row))
matrix))
It works as expected:
(transform-to-sqr '((1 2) (3 -2)))
=> '((1 4) (9 4))
(transform-to-sqr '((3 4 5) (2 1 3) (2 3 7) (0 -3 2)))
=> '((9 16 25) (4 1 9) (4 9 49) (0 9 4))
This question already has an answer here:
Nested Loops Using Loop Macro in Common Lisp
(1 answer)
Closed 2 years ago.
I want to create a list of pairs (a . b) with 1 < a < b <= n up to n, e.g. n = 5:
((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))
(The order of the pairs is not critical.)
I came up with the code
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound
for j from 2 to (1- upper-bound)
collecting (cons j i)))
but that doesn't do what I wanted
* (create-pairs 5)
((2 . 3) (3 . 4) (4 . 5))
since the loops are incrementing at the same time.
Therefore I tried this
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound do
(loop for j from 2 to (1- upper-bound)
collecting (cons j i))))
with the result:
* (create-pairs 5)
NIL
I don't remember where but I read that it's not possible to use collecting in constructs like my second try.
So how do I get the result I want to have? Isn't it possible to solve this with loop for?
You are almost there - you just need to accumulate the results of the inner loop:
(defun create-pairs (upper-bound)
(loop for i from 3 to upper-bound nconc
(loop for j from 2 below i
collect (cons j i))))
(create-pairs 5)
==> ((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))
I'm learning Racket and I want to get the second list of a list of lists, not as a list of a list of lists.
I have this list of lists:
(define list-of-list '(((a b c) (d e f)) ((1 2 3) (4 5 6))))
If I do:
(car list-of-list)
I get:
> '((a b c) (d e f))
But, if I do:
(cdr list-of-list)
I get:
> '(((1 2 3) (4 5 6)))
I have also tried:
(list-tail list-of-list 1)
But I get a list of a list of lists:
>'(((1 2 3) (4 5 6)))
The only way to get what I want is doing:
(cadr list-of-list)
> '((1 2 3) (4 5 6))
I think this is the right working way of Racket, but, because I'm learning:
Is there a better (or functional programming style) way to do it?
It's the correct way, but this is easier to read:
(second list-of-list)
=> '((1 2 3) (4 5 6))
In case you're wondering, there is also third and fourth and so on up until tenth. It's all in the docs.
Can I recommend either "The Little Schemer" (amusing, short, thought-provoking) or "How To Design Programs" (free on the web at HtDP.org, long and detailed, textbook-ish)? Both are well-written by experts. In fact, one expert appears in the author list of both books.
A function that receives a list with sublist's M*N and returns the sum of all elements of the sublist's
example: (solution '( (1 2 3) (4 5 6) ) )
return: (5 7 9)
Sry for the bad english
thks =)
A Common Lisp version is almost the same as in Scheme:
(defun solution (list)
(apply #'mapcar #'+ list))
(solution '((1 2 3) (9 10 11) (3 4 5)))
; ==> (13 16 19)
It depends on what Lisp interpreter you're using. In Scheme, this will work:
(define (solution lsts)
(apply map + lsts))
For example:
(solution '((1 2 3) (4 5 6)))
=> '(5 7 9)
I'm having a bit of trouble with Lisp. What i'm attempting to do, is keep track of the amount of times a number appears in x number of lists. However, running this over and over again, lisp isn't recreating the variable, but using the ending value from the last time I called the function. So I'm wondering how can I get past the 'binding' powers of let?
So, I've got some list like this
(((8 7) (3)) ((8 3) (2)) ((7 3) (6)) ((7 2) (8)) ((6 7) (4 1))
((6 6) (4 1)) ((6 2) (2)) ((5 6) (3)) ((5 3) (8 3)) ((4 6) (4))
((4 4) (6)) ((4 1) (7)) ((3 7) (5 3)) ((3 4) (1)) ((3 3) (3)) ((3 1) (9))
((2 7) (7)) ((2 5) (2)) ((2 2) (5 2)) ((1 7) (1)) ((1 6) (6 1))
((1 1) (2 1)) ((1 0) (3)) ((0 7) (8 1)) ((0 5) (6)) ((0 3) (9 6))
((0 1) (1)))
Then I'm calling some function like this, (declaring var here doesn't seem to do anything, past the initial function call)... I guess some kind of binding from let.
(defun counter (possibleValues)
(let ((var '(0 0 0 0 0 0 0 0 0 0)))
(loop for i from 0 to (list-length possibleValues) do
(loop for j in (cdr (nth i possibleValues)) do
(loop for k in j do
(incf (nth k var)))))
var))
So I can run my list through the function and get something like
(0 8 5 6 3 2 5 2 3 2)
Each position referring to the number found in the list. So the value 8 would refer to how many times 1 was found in all the lists (i'm considering the second list only). Now the problem.... run it twice and...
(0 16 10 12 6 4 10 4 6 4)
I was using an associative list earlier, but in trying to figure this out and keep things simple, i'm now using a list. I guess another question I have is, how can I create associative list elements on the fly? I don't like declaring 'var' like that, but I'm just trying to get around 'let' for the moment. I haven't had much luck with 'setq' or 'setf' either....
Thanks in advance for your help!
Change the initialization form for VAR to be an expression that creates new lists, such as (make-list 10 :initial-element 0) or even (list 0 0 0 0 0 0 0 0 0 0).
Basically, do not ever use quoted objects if you have intentions on modifying them, as the consequences are undefined if you do. In fact, evaluating that function definition gives a warning about that:
; in: LAMBDA NIL
; (INCF (NTH K VAR))
; --> LET*
; ==>
; (SB-KERNEL:%SETNTH #:TMP5 #:TMP4 #:NEW3)
;
; caught WARNING:
; Destructive function SB-KERNEL:%SETNTH called on constant data.
; See also:
; The ANSI Standard, Special Operator QUOTE
; The ANSI Standard, Section 3.2.2.3
;
; compilation unit finished
; caught 1 WARNING condition