Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: (untagged)
As shown below, I have the load-mobile-plan function which iterate the text and construct the nth and n+1 lines(n = odd) into a pair, it then append it to a list.
(define temp-pair '())
(define mobile-plan '())

(define load-mobile-plan
  (lambda (file)
    (let [(line (read-line file))]
      (unless (eof-object? line)
        (set! temp-pair (cons line (read-line file)))
        (set! mobile-plan (cons temp-pair mobile-plan))
        (load-mobile-plan file)))))

(call-with-input-file "xxx.txt" load-mobile-plan)


My questions:
-Is this an appropriate method in functional programming? As i heard that function in functional programming should not have side-effects and depends on its own input variables only.
-If so, how to modify my codes to achieve same goal?
Posted
Updated 30-Oct-12 3:29am
v2

1 solution

OK after few days of "investigation" into functional programming, i manage to code the following, which do the same job as my codes in question:
(define load-mobile-plan
  (lambda (file)
    (let [(mobile-plan '())]
      (read-file file mobile-plan))))

(define read-file
  (lambda (file mobile-plan)
    (let [(line (read-line file))]
      (if [eof-object? line]mobile-plan
          (let* [(temp-pair (cons (string-normalize-spaces line)
                                  (string-normalize-spaces (read-line file))))
                 (mobile-plan (cons temp-pair mobile-plan))]
            (read-file file mobile-plan))))))

(call-with-input-file "xxx.txt" load-mobile-plan)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900