Click here to Skip to main content
13,048,406 members (105,652 online)
Rate this:
Please Sign up or sign in to 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 29-Oct-12 22:35pm
Updated 30-Oct-12 3:29am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100