scheme - Multiple procedures within a racket conditional? -


i working on project euler problem # 3 in racket , not sure how use multiple procedures 1 procedure in conditionals. when using procedural language, i'd use 'while' loop , variable updating (hence why use 'set!' in following code (if there better way- i've read related questions/answers it's not practice mutate variables in racket, please offer alternative) , have several procedures if conditional true. putting multiple instructions 1 output statement (by enclosing in '()' doesn't seem work sure there way accomplish this.

i've considered breaking small functions , making 2 larger functions (1 'then' area , 1 'else area) doesn't seem right solution (or @ least not conventional approach).

i have included error message specific code if helps.

; project euler # 3 ; largest prime factor of 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 procedure;  expected procedure can applied arguments   given: '(() 11)   arguments.: 

i've made 'then' , 'else' areas in bold (if doesn't come out in bold, surrounded ** ** (example: example). won't ruin readability.

thank you.

to group multiple instructions in 1 statement can use begin.

(if (= (remainder dividend divisor) 0)     (begin (append pl (list divisor)) //             (set! dividend (/ dividend divisor))            (set! divisor 2)            (pf dividend))     (begin (set! divisor (+ divisor 1)) // else            (pf dividend))) 

note return value of last instruction result of (begin ...).

however running code these changes produce '(()). because (append pl (list divisor)) return new list. hence instruction useless because aren't catching result.

i suppose want (set! pl (append pl (list divisor))). won't return empty list anymore.

you want pl variable defined (define pl '()). first element of result won't empty list anymore (which caused first append).

i tested modified code, should work :)


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -