I would like to use a (range x y) in a case statement rather than listing the full range, is this possible?
I believe that the (range 1 4) in the second example is returning a list, and since 2 != the list, there is no match.
> (case 2
[(1 2 3) "matched"]
[else "no match"])
"matched"
> (case 2
[(range 1 4) "matched"]
[else "no match"])
"no match"
Vanilla case will not work. The LHS of each branch of case is really limited to literals.
You can use other forms, like cond or match. E.g.,
(match 2
[-1 'foo]
[0 'bar]
[x #:when (member x (range 1 3)) 'baz]
[x #:when (member x (range 3 5)) 'boo])
This comes at the cost that it won't be as efficient as using case.
Another approach is to write a macro that expands to case, like:
#lang racket
(require syntax/parse/define
(for-syntax racket/list))
(begin-for-syntax
(define-syntax-class range-cls
(pattern (#:range l:number r:number)
#:with result (range (syntax-e #'l) (syntax-e #'r)))))
(define-simple-macro (case-range v [rgs:range-cls rhs ...] ...)
(case v [rgs.result rhs ...] ...))
(case-range 2
[(#:range -1 1) 'foo]
[(#:range 1 3) 'bar]
[(#:range 3 5) 'baz])
It isn't possible, because case uses implicit quote. In your example, it quotes all elements in list (range 1 4) and compares them (using equal?) with 2.
(case 2
[(range 1 4) "matched"]
[else "no match"])
;2 is compared with 'range, 1, 4 => "no match"
(case 'range
[(range 1 4) "matched"]
[else "no match"])
;'range is compared with 'range, 1, 4 => "matched"
You can use cond here:
(let ((v 2))
(cond ((member v (range 1 4)) "matched 1-4")
((member v (range 4 8)) "matched 4-8")
(#true "no match")))
Related
Is there a way I can generate multiple values in for/list during each iteration and have the results "flattened"?
For instance:
(for/list ([i (range n)]) (values i (+ i 1)))
I'd like the result to be (list 0 1 1 2 2 3 3 4 ...).
This question is very much relevant to https://github.com/racket/racket/pull/2483. In the link, you will find:
An unmerged PR that lets you write (for/append-list ([i (range n)]) (list i (+ i 1))).
My proposal to let you write (for/list* ([i (range n)]) (values i (+ i 1))) (with caveat, see the link for more details).
But since these don't exist in Racket yet, the easiest way to get what you want is:
(append* (for/list ([i (range n)]) (list i (+ i 1))))
Clojure's ->> macro thread the form from the last argument, when -> form from the first.
user=> (->> a (+ 5) (let [a 5]))
10
However, I get an exception when I used the operations exchanged.
user=> (-> a (let [a 5]) (+ 5))
CompilerException java.lang.IllegalArgumentException: let requires a vector for its binding in user:1, compiling:(NO_SOURCE_PATH:1:7)
Furthermore, I expect these two operations will get me the same results, which is not.
user=> (-> 0 (Math/cos) (Math/sin))
0.8414709848078965
user=> (->> 0 (Math/sin) (Math/cos))
1.0
What's wrong? How's the -> and ->> macros work?
The -> macro inserts the argument as the first argument for the given function, not giving the argument to the last function.
Likewise ->> inserts as the last argument.
user=> (macroexpand '(-> x (- 1)))
(- x 1)
user=> (macroexpand '(->> x (- 1)))
(- 1 x)
Two simple examples:
user=> (-> 1 (- 1) (- 2))
-2
user=> (->> 1 (- 1) (- 2))
2
As for the first example, -2 == (- (- 1 1) 2), and for the second 2 == (- 2 (-1 1))
As a result, we get the same results for the unary functions.
user=> (macroexpand '(-> 0 Math/sin Math/cos))
(. Math cos (clojure.core/-> 0 Math/sin))
user=> (macroexpand '(->> 0 Math/sin Math/cos))
(. Math cos (clojure.core/->> 0 Math/sin))
So, only ->> makes sense in the question.
user=> (macroexpand '(->> a (+ 5) (let [a 5])))
(let* [a 5] (clojure.core/->> a (+ 5)))
user=> (macroexpand '(-> a (+ 5) (let [a 5])))
IllegalArgumentException let requires a vector for its binding in user:1 clojure.core/let (core.clj:4043)
I have two lists: (1 2 3) and (a b) and I need to create something like this (1 2 3 1 2 3). The result is a concatenation of the first list as many times as there are elements in the second. I should use some of the functions (maplist/mapcar/mapcon, etc.). This is exactly what I need, although I need to pass first list as argument:
(mapcan #'(lambda (x) (list 1 2 3)) (list 'a 'b))
;=> (1 2 3 1 2 3)
When I try to abstract it into a function, though, Allegro freezes:
(defun foo (a b)
(mapcan #'(lambda (x) a) b))
(foo (list 1 2 3) (list 'a 'b))
; <freeze>
Why doesn't this definition work?
There's already an accepted answer, but I think some more explanation about what's going wrong in the original code is in order. mapcan applies a function to each element of a list to generate a bunch of lists which are destructively concatenated together. If you destructively concatenate a list with itself, you get a circular list. E.g.,
(let ((x (list 1 2 3)))
(nconc x x))
;=> (1 2 3 1 2 3 1 2 3 ...)
Now, if you have more concatenations than one, you can't finish, because to concatenate something to the end of a list requires walking to the end of the list. So
(let ((x (list 1 2 3)))
(nconc (nconc x x) x))
; ----------- (a)
; --------------------- (b)
(a) terminates, and returns the list (1 2 3 1 2 3 1 2 3 ...), but (b) can't terminate since we can't get to the end of (1 2 3 1 2 3 ...) in order to add things to the end.
Now that leaves the question of why
(defun foo (a b)
(mapcan #'(lambda (x) a) b))
(foo (list 1 2 3) '(a b))
leads to a freeze. Since there are only two elements in (a b), this amounts to:
(let ((x (list 1 2 3)))
(nconc x x))
That should terminate and return an infinite list (1 2 3 1 2 3 1 2 3 ...). In fact, it does. The problem is that printing that list in the REPL will hang. For instance, in SBCL:
CL-USER> (let ((x (list 1 2 3)))
(nconc x x))
; <I manually stopped this, because it hung.
CL-USER> (let ((x (list 1 2 3)))
(nconc x x) ; terminates
nil) ; return nil, which is easy to print
NIL
If you set *print-circle* to true, you can see the result from the first form, though:
CL-USER> (setf *print-circle* t)
T
CL-USER> (let ((x (list 1 2 3)))
(nconc x x))
#1=(1 2 3 . #1#) ; special notation for reading and
; writing circular structures
The simplest way (i.e., fewest number of changes) to adjust your code to remove the problematic behavior is to use copy-list in the lambda function:
(defun foo (a b)
(mapcan #'(lambda (x)
(copy-list a))
b))
This also has an advantage over a (reduce 'append (mapcar ...) :from-end t) solution in that it doesn't necessarily allocate an intermediate list of results.
You could
(defun f (lst1 lst2)
(reduce #'append (mapcar (lambda (e) lst1) lst2)))
then
? (f '(1 2 3) '(a b))
(1 2 3 1 2 3)
Rule of thumb is to make sure the function supplied to mapcan (and destructive friends) creates the list or else you'll make a loop. The same applies to arguments supplied to other destructive functions. Usually it's best if the function has made them which makes it only a linear update.
This will work:
(defun foo (a b)
(mapcan #'(lambda (x) (copy-list a)) b))
Here is some alternatives:
(defun foo (a b)
;; NB! apply sets restrictions on the length of b. Stack might blow
(apply #'append (mapcar #'(lambda (x) a) b))
(defun foo (a b)
;; uses loop macro
(loop for i in b
append a))
I really don't understand why b cannot be a number? You're really using it as church numbers so I think I would have done this instead:
(defun x (list multiplier)
;; uses loop
(loop for i from 1 to multiplier
append list))
(x '(a b c) 0) ; ==> nil
(x '(a b c) 1) ; ==> (a b c)
(x '(a b c) 2) ; ==> (a b c a b c)
;; you can still do the same:
(x '(1 2 3) (length '(a b))) ; ==> (1 2 3 1 2 3)
In Arc there's a macro called in
> (let x 1
(in x 4 5 6))
nil
> (let x 1
(in x 1 5 6))
t
that checks if its first parameter is equal to any of the rest. I want a version of this that takes a parameter plus a list (semantically identical to Python's in), so I wrote:
(assign weak-tens* '(11 12))
(mac in? (elt lst)
(cons 'in (cons elt lst)))
(def transform-deck (deck)
(map [if (in? _ weak-tens*) #\T _] deck))
output:
arc> (load "main.arc")
*** redefining in?
map: contract violation
expected: list?
given: '(_ . weak-tens*)
argument position: 2nd
other arguments...:
#<procedure:ac-niltree>
To answer your immediate question, you can use mem, as follows:
arc> (let x 3
(mem x '(1 2 3 4)))
(3 4)
Also, there's no reason this should be a macro. It doesn't do anything that requires a macro.
But let's look at why the macro doesn't work:
arc> (macex1 '(in? 1 '(1 2 3)))
(in 1 quote (1 2 3))
Ah, we're putting the value "quote".
Here's how we want the code to expand:
(in? 1 '(1 2 3))
should expand to:
(in 1 1 2 3)
But as mentioned, we don't even want this to be a macro in the first place. Ignoring mem, it could be written as follows:
(def in? (elt lst)
(if (no lst)
nil
(is elt ;;if we've found the element
(car lst))
t
(in? elt (cdr lst)))) ;;otherwise recurse
I have written the following program to calculate the sum of all multiples of 3 & 5 below 1000 in scheme. However, it gives me an incorrect output.
Any help would be much appreciated.
(define (multiples)
(define (calc a sum ctr cir)
(cond (> a 1000) (sum)
(= ctr 7) (calc (+ a (list-ref cir 0)) (+ sum a) 0 (list 3 2 1 3 1 2 3))
(else (calc (+ a (list-ref cir ctr)) (+ sum a) (+ 1 ctr) (list 3 2 1 3 1 2 3)))))
(calc 0 0 0 (list 3 2 1 3 1 2 3)))
You can simply port imperative style solution to functional Scheme by using an accumulator(sum parameter) and a target parameter to test when to stop summing:
(define (multiples)
(define (multiples-iter num sum target)
(if (> num target)
sum
(multiples-iter (+ 1 num)
(if (or (zero? (mod num 3)) (zero? (mod num 5)))
(+ sum num)
sum)
target)))
(multiples-iter 0 0 1000))
Here's my (Racket-specific) solution, which doesn't involve lots of (or, for that matter, any) modulo calls, and is completely general (so that you don't need to construct the (3 2 1 3 1 2 3) list that the OP has):
(define (sum-of-multiples a b limit)
(define (sum-of-multiple x)
(for/fold ((sum 0))
((i (in-range 0 limit x)))
(+ sum i)))
(- (+ (sum-of-multiple a) (sum-of-multiple b))
(sum-of-multiple (lcm a b))))
Test run:
> (sum-of-multiples 3 5 1000)
233168
If you're using Racket, there's a very compact way to do what you ask, using looping constructs:
(for/fold ([sum 0])
([i (in-range 1 1000)]
#:when (or (zero? (modulo i 3)) (zero? (modulo i 5))))
(+ sum i))
=> 233168
One problem is that your code is missing a pair of parentheses around the cond clauses.
In the line (cond (> a 1000) (sum) the condition is just> while a and 1000 are interpreted as forms to be evaluated if > is true (which it is), and thus 1000 will be returned as the result.
Two other problem (masked by the first one) is that you are initializing ctr to 0 when it reaches 7, while it should be set to the next value, i.e. 1, and that you are including 1000 in the result.
The corrected version of your function is
(define (multiples)
(define (calc a sum ctr cir)
(cond ((>= a 1000) sum)
((= ctr 7) (calc (+ a (list-ref cir 0)) (+ sum a) 1 (list 3 2 1 3 1 2 3)))
(else (calc (+ a (list-ref cir ctr)) (+ sum a) (+ 1 ctr) (list 3 2 1 3 1 2 3)))))
(calc 0 0 0 (list 3 2 1 3 1 2 3)))
The same algorithm can also be defined as a non-recursive function like this:
(define (multiples)
(do ((cir (list 3 2 1 3 1 2 3))
(ctr 0 (+ ctr 1))
(a 0 (+ a (list-ref cir (modulo ctr 7))))
(sum 0 (+ sum a)))
((>= a 1000) sum)))
(require-extension (srfi 1))
(define (sum-mod-3-5 upto)
(define (%sum-mod-3-5 so-far generator-position steps)
(let ((next (car generator-position)))
(if (> (+ steps next) upto)
so-far
(%sum-mod-3-5 (+ so-far steps)
(cdr generator-position)
(+ steps next)))))
(%sum-mod-3-5 0 (circular-list 3 2 1 3 1 2 3) 0)) ; 233168
For this particular task, it will do on average half the operations then you would do if incrementing the counter by one, also, one less if condition to check.
Also, modulo (as being division in disguise, probably) is more expensive then summation.
EDIT: I'm not a pro on modular system in different dialects of Scheme. The SRFI-1 extension here is only required to make it easier to create a circular list. I couldn't find an analogue to Common Lisp (#0=(3 2 1 3 1 2 3) . #0#), but perhaps, someone more knowledgeable will correct this.
If you absolutely want to use the "repeating pattern" method, you could go about it something like this.
This uses recursion on the list of intervals rather than relying on list-ref and explicit indexing.
(define (mults limit)
(define steps '(3 2 1 3 1 2 3))
(define (mults-help a sum ls)
(cond ((>= a limit) sum)
((null? ls) (mults-help a sum steps))
(else (mults-help (+ a (car ls))
(+ a sum)
(cdr ls)))))
(mults-help 0 0 steps))