Click here to Skip to main content
11,812,712 members (73,266 online)
Rate this: bad
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
Edited 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
0 Abhinav S 205
1 CHill60 180
2 KrunalRohit 169
3 Palash Mondal_ 140
0 OriginalGriff 3,877
1 KrunalRohit 2,496
2 CPallini 2,240
3 Maciej Los 2,205
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2015
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