Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: list Append
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 23:35pm
tuolesi797
Edited 30-Oct-12 4:29am
v2

1 solution

Rate this: bad
good
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)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Nov 2012
Copyright © CodeProject, 1999-2014
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