Common Lisp `loop`: maximize into local variable introduced by `let`
369 观看
1回复
The loop
facility in Common Lisp allows several value accumulation clauses, maximize
amongst others.
Now, it is also possible to give a variable var
to the maximize
clause:
(loop for x from 0 to 10 maximize (func x) into var)
My question is:
Is it possible to give as var
a new local variable introduced by let
?
An example scenario would be:
(let ((var -1)) ; assume numeric result
(loop for x from 0 to 10 maximize (func x) into var))
It is not important that x
has a numeric value, it's only for illustration purposes.
回应 (1)
7像
Mix bindings?
No, the into
variables are bound by loop
.
What you can do is bind your var
to the return value of loop
:
(let ((var (loop for x from 0 to 10 maximize (func x))))
;; use var here
...)
Complex loop - use multiple values, functional style
If you are doing many things in a single loop, you might want to use values function in Common Lisp:
(multiple-value-bind (max min sum)
(loop for x from 0 to 10
maximize (f1 x) into max
minimize (f2 x) into min
sum (f3 x) into sum
finally (return (values max min sum)))
;; use max, min and sum here
...)
Note that the variables max
, min
and sum
bound by multiple-value-bind
and loop
are completely separate and independent, and have absolutely nothing in common and are named the same for didactic purposes only.
If you rename them (as you definitely should for the sake of code readability!):
(multiple-value-bind (max min sum)
(loop for x from 0 to 10
maximize (f1 x) into max1
minimize (f2 x) into min1
sum (f3 x) into sum1
finally (return (values max1 min1 sum1)))
;; use max, min and sum here
...)
and recompile your code, you will see that the disassembly is identical.
Complex loop, use finally
, procedural style
As suggested by @coredump, you can set your variables in the finally
construct:
;; bind max, min and sum
(loop for x from 0 to 10
maximize (f1 x) into max1
minimize (f2 x) into min1
sum (f3 x) into sum1
finally (setq max max1
min min1
sum sum1))
;; use max, min, and sum; max1 et al do not exist here
Generally, speaking, there is more than one way to skin the cat here...
作者: sds 发布者: 26.12.2017 06:38来自类别的问题 :
- loops 关于动态的Haxe迭代
- loops Python 2.X中range和xrange函数有什么区别?
- loops 如何在C#中枚举枚举?
- loops 迭代字典的最佳方法是什么?
- loops 应该尝试......赶上环路内外?
- common-lisp Lisp可执行文件
- common-lisp Common Lisp中的动态和词法变量
- common-lisp Common Lisp中的LET与LET *
- common-lisp 各种ANSI CL实现有何不同?
- common-lisp 解决斐波纳契的方法
- local-variables MySQL SELECT,存储在变量中
- local-variables 如何在SQL Server中声明变量并在同一存储过程中使用它
- local-variables 在if语句中初始化变量的范围是什么?
- local-variables 如何使用指针从其他函数访问局部变量?
- local-variables 从C中的函数返回局部变量
- let-binding Common Lisp `loop`: maximize into local variable introduced by `let`