Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Problem w/ FP

Name: Anonymous 2012-02-09 22:49

Functional programing makes simple things nearly imposible to do . for example I found this exercise on reddit

"create a program that will ask the users name, age, and reddit username. have it tell them the information back, in the format:
your name is (blank), you are (blank) years old, and your username is (blank)
for extra credit, have the program log this information in a file to be accessed later."

At first I dismissed it as stupidly trivial but then thought, is there even a way to do this in scheme? And I couldnt think of one. This just shows that scheme sucks for programs used by users. There is no sequential steps to actually accomplish anything

Name: Anonymous 2012-02-10 2:18

Did I do it right?
#lang scheme

(define prog
  "create a program that will ask the users name, age, and reddit username. have it tell them the information back, in the format:
 your name is (blank), you are (blank) years old, and your username is (blank)
 for extra credit, have the program log this information in a file to be accessed later.")

(define lines (regexp-split " *\n *" prog))

(let grok ( (cur    (first lines))
            (rst    (rest lines))
            (cprog '())
            (idnts '()) )
  (let ((a (regexp-replace* "[,.:]" cur " \\0 ")))
    (let nlpread ((b     (map string->symbol (regexp-split " +" a)))
                  (cprog cprog)
                  (idnts idnts))
      (match b
        (`(create a program ,etc ... )
         (nlpread etc cprog idnts) )
        (`(that will ask ,etc ...)
         (nlpread etc cprog idnts) )
        (`(the users ,etc ...)
         (let nlpreadlist ((stf etc) (cprog cprog) (idnts idnts))
           (define (make-statements identifier)
             (define scmident (string->symbol (string-append* (add-between (map symbol->string identifier) "-"))))
             (values
              `((printf ,(format "Your ~a:~~n" (string-append* (add-between (map symbol->string identifier) " "))))
                (define ,scmident (read))) scmident) )
           (match stf
             (`(,(and (not '|.|) (not '|,|) identifier) ... |,| ,etc ...)
              (define-values (stmt idnt) (make-statements identifier))
              (nlpreadlist etc (append cprog stmt) (append idnts (list idnt))) )
             (`(and ,(and (not '|,|) identifier) ... |.| ,etc ...)
              (define-values (stmt idnt) (make-statements identifier))
              (nlpread     etc (append cprog stmt) (append idnts (list idnt))) ))))
        (`(have it tell them the information back |,| in the format |:| ,_ ...)
         (define fmtstr (regexp-replace* "\\(blank\\)" (first rst) "~a"))
         (grok (first (rest rst)) (rest (rest rst))
               (append cprog `((printf ,fmtstr ,@idnts)))
               idnts) )
        (`(for extra credit |,| ,etc ...)
         (nlpread etc cprog idnts) )
        (`(have the program log this information in a file ,etc ...)
         (define logfmt (string-append* (add-between (build-list (length idnts) (lambda (x) "~a")) "\t")))
         (nlpread etc
                 (append cprog `((call-with-output-file "log" (lambda (fo) (fprintf fo ,(format "~a~~n" logfmt) ,@idnts)) #:exists 'append)))
                 idnts) )
        (jank
         (define ns (make-base-namespace))
         (eval (cons 'begin cprog) ns)) ))))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List