【发布时间】:2015-11-24 23:12:30
【问题描述】:
我正在研究球拍中的 Project Euler 问题 #3,但不确定如何在条件句中使用多个程序作为一个程序。通常在使用过程语言时,我会使用“while”循环并进行一些变量更新(因此为什么我在下面的代码中使用“set!”(如果有更好的方法——正如我从相关问题中读到的那样) /answers that it's not good practice to mutate variables in racket, then please provide an alternative) and have several processes if a condition is true. 将多条指令作为一个输出语句(通过将其包含在“()”中似乎不是工作,但我相信有一些方法可以做到这一点。
我考虑过将其分解为小功能,然后制作 2 个更大的功能(1 个用于“then”区域,1 个用于“else”区域)但它似乎不是正确的解决方案(或者至少不是传统方法)。
如果有帮助,我还包含此特定代码的错误消息。
; Project Euler # 3
; What is the largest prime factor of the number 600851475143?
(define pL (list null))
(define divisor 2)
(define (pF dividend)
(if (= dividend 1)
pL
(if (= (remainder dividend divisor) 0)
**(**(append pL (list divisor))
(set! dividend (/ dividend divisor))
(set! divisor 2)
(pF dividend)**)**
**(**(set! divisor (+ divisor 1))
(pF dividend)**)**)))
(pF 33)
application: not a procedure;
expected a procedure that can be applied to arguments
given: '(() 11)
arguments.:
我已将“then”和“else”区域设为粗体(如果它没有以粗体显示,那么它将被 ** ** 包围(例如:example ). 希望它不会破坏可读性。
谢谢。
【问题讨论】:
标签: scheme conditional racket