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

Pages: 1-4041-

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-09 23:01

Back to Reddit, please!

Name: Anonymous 2012-02-09 23:07

Erlang

Name: Anonymous 2012-02-09 23:18

Wow, I'm surprised, this is pretty trivial in scheme. The only thing that's a little difficult is the file io, not because of functional style, but because it is different from C,C++,Java, and it I don't use file io with scheme very often. I'll have to look it up again.


;; for-each is a built in on my scheme implementation.
;(define (for-each operator lis)
;  (if (not (null? lis))
;    (begin (operator (car lis))
;           (for-each operator (cdr lis)))))

(define (print-user-info name age reddit-username)
  (for-each display (list "your name is " name ", you are " age " years old, and your username is " reddit-username))
  (newline))

(define (main log-file)
  (let* ((name (begin (display "Please enter your name: ") (read)))
         (age  (begin (display "Please enter your age: ") (read)))
         (reddit-username (begin (display "Please enter your reddit user name: ") (read))))
    (print-user-info name age reddit-username)
    ;(save-user-info-to-log-file log-file name age reddit-user-name) ;; Lemme look up scheme file io again...
    ))

(main #f)



(load "read.scm")
;loading read.scm
Please enter your name: timmy
Please enter your age: 5634
Please enter your reddit user name: ti_mmy
your name is timmy, you are 5634 years old, and your username is ti_mmy
;done loading read.scm
#<unspecified>

Name: Anonymous 2012-02-09 23:41

Frame pointers are necessary

Name: Anonymous 2012-02-10 0:01

>>4
)))))

Name: Anonymous 2012-02-10 0:15

REDDIT

Name: Anonymous 2012-02-10 0:23

Functional programing makes simple things nearly imposible to do .
I'm sure you are completely qualified to make that statement.

is there even a way to do this in scheme?
You're trolling, right? Of course it's possible in Scheme, Scheme isn't even purely functional.

And I couldnt think of one.
That's not Scheme's fault, it's yours.

Here's a solution in Haskell, which is purely functional:
import Control.Monad

main :: IO ()
main = do putStrLn "What is your name?"
          name <- getLine
          putStrLn "What is your age?"
          age <- readLn
          when (age > 12) $ error "stop lying, ``faggot''"
          putStrLn $ "Your name is " ++ name ++ ", you are " ++ show age ++
                     " years old and you are a massive faggot."


IHBT.

Name: Anonymous 2012-02-10 0:26

>>8
Haskell is fucking shit.

Name: Anonymous 2012-02-10 0:30

>>9
It's still better than Java.

Name: Anonymous 2012-02-10 0:30


;; for-each is a built in on my scheme implementation.
;(define (for-each operator lis)
;  (if (not (null? lis))
;    (begin (operator (car lis))
;           (for-each operator (cdr lis)))))

(define (print-user-info name age reddit-username)
  (for-each display (list "your name is " name ", you are " age " years old, and your username is " reddit-username))
  (newline))


(define (make-user-info-writer name age reddit-username)
  (lambda (out-file)
    (write (list name age reddit-username) out-file)))
;    (for-each (lambda (str) (display str out-file))
;              (list name age reddit-username))))


(define (main log-file)
  (let* ((name (begin (display "Please enter your name: ") (read)))
         (age  (begin (display "Please enter your age: ") (read)))
         (reddit-username (begin (display "Please enter your reddit user name: ") (read))))
    (print-user-info name age reddit-username)
    ((make-user-info-writer name age reddit-username) log-file)))

(call-with-output-file "user.tt" main)


this seems to do it, except it automatically downcases all strings, which is not ideal.


(load "read.scm")
;loading read.scm
Please enter your name: Patty
Please enter your age: 52
Please enter your reddit user name: PaTtY
your name is patty, you are 52 years old, and your username is patty
;done loading read.scm
#<unspecified>
;EXIT
$ cat user.tt
(patty 52 patty)$

Name: Anonymous 2012-02-10 0:34

>>5
and they can still be used in functional programming language implementations, fool. Objects need only be allocated on the heap when it cannot be proven that it's life time will stay contained within the life of the function call, ie, there can be no references to the object after the function returns.

Name: Anonymous 2012-02-10 0:35

>>9
why?

Name: Anonymous 2012-02-10 0:36

(> cl scheme)

Don't feel like doing the log file.

(defun prompt (message)
  (format t "~A " message)
  (read))

(defun main ()
  (format t "Your name is ~A, you are ~A years old and your reddit user name (faggot) is ~A." (prompt "What is your name?") (prompt "How old are you?") (prompt "What is your reddit user name? (faggot)")))

Name: Anonymous 2012-02-10 2:13

#include <stdio.h>
int main(void) {
    char *name, *username;
    unsigned age;
    printf("What is your name?\n");
    scanf("%m[^\n]", &name);
    printf("What is your age?\n");
    scanf("%u%*c", &age);
    printf("What is your username?\n");
    scanf("%m[^\n]", &username);
    printf("Your name is %s, you are %u years old and your username is %s.", name, age, username);
    free(name);
    free(username);
}

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)) ))))

Name: Anonymous 2012-02-10 3:06

ENTERPRISE SOLUTIONS
TextBoardFactoryFactory textBoardFactoryFactory = new TextBoardFactoryFactory();
TextBoardFactory fourChan = textBoardFactoryFactory.getTextBoardFactory(TextBoardSites.fourChan.toString());
TextBoard prog = fourChan.getTextBoard(TextBoards.yotsuba.slashProgSlash.toString());
TextBoardThreadID myThread = prog.nextThreadID();
TextBoardThreadCreator poster = new TextBoardThreadCreator(prog.getBoardName().getHash());
try {
     poster.post(myThread.getIndex(),
          new TextBoardMessage(TextBoardMessagePosters.getPosterID(TextBoardMessagePosters.Anonymous).toPosterName(),
          new TextBoardThreadSubject("program for a humble Redditor"),
          TextBoardThreadTools.messageEmails.nullEmailID.toEmail(),
          "create a program that will ask the users name, age, and reddit username. have it tell them the information back, in the format:\n" +
          "your name is (blank), you are (blank) years old, and your username is (blank)\n" +
          "for extra credit, have the program log this information in a file to be accessed later.").uriEncode());
} catch (Exception e) {
     Logger logger = LoggerFactory.getLogger(TextBoardThreadTools.getloggerCode(TextBoards.yotsuba.slashProgSlash.toString()));
     logger.log("Post Failed: " + e.getMessage());
}

Name: Anonymous 2012-02-10 3:25

>>15
>passing uninitialized pointers

What do you expect to happen, honestly?

Name: Anonymous 2012-02-10 3:29

>>4
Good sir inclusive or madam, you have been trolled.

Name: Anonymous 2012-02-10 3:29


1> c(reddit).   
{ok,reddit}
2> reddit:main().
Name: Your mom     
Age: 16           
Reddit username: IHBT         
your name is Your mom, you are 16 years old, and your username is IHBT
ok
4> _

Name: Anonymous 2012-02-10 3:31

>>20
Well, I guess you want the source, too...


-module(reddit).
-export([main/0]).

main() ->
    Name = io:get_line("Name: "),
    Age = io:get_line("Age: "),
    Username = io:get_line("Reddit username: "),
    io:fwrite("your name is ~s, you are ~s years old, and your username is ~s\n", lists:map(fun(X) -> string:strip(X, right, $\n) end, [Name, Age, Username])).

Name: Anonymous 2012-02-10 3:33

>>18
Read up on the POSIX-2008 scanf m flag. It will crash on Windows though.

Name: Anonymous 2012-02-10 3:41

>>15
Good god read about pointers and memory management in C, sir.

Name: Anonymous 2012-02-10 8:04

>>21
i like how the only actual funtional part is the one where the newline-character is stripped from the strings

Name: Anonymous 2012-02-10 8:23

>>23
See:
>>22

Name: Anonymous 2012-02-10 8:48

>>25
freeing local variables allocated on the stack has nothing to do with POSIX or scanf.  It has everything to do with not having a clue about memory management.

Name: Anonymous 2012-02-10 8:51

>>2
Who really cares anymore?

IT'S A FRIGGING site.

Name: Anonymous 2012-02-10 9:29

>>10
Anything is.

>>14
Despite CL being shit, that's actually fairly decent.

>>15
That must be the worst code I've ever read.

>>16
Never mind, this is the worst code I've ever read.

>>17
I just puked a little in my mouth.

>>21
lists:map?  Really?

Name: Anonymous 2012-02-10 10:01

>>26
Are you a middle schooler from Yahoo! Answers or are you the guy who gets 50 downvotes on Stack Overflow because you refuse to read the man page even though you have no clue what you're talking about? Seriously RTFM.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html

Name: Anonymous 2012-02-10 10:22

>>14
Just bind *standard-output* to your logging file while calling your function and you have yourself free log file output at no cost!

Name: Anonymous 2012-02-10 11:41

>>28
What else would you use map on?

Name: Anonymous 2012-02-10 12:28

>>27
I care, faggot

Name: Anonymous 2012-02-10 12:28

>>4
This is ridiculously long and over-complicated.

Functional is shit.

Name: Anonymous 2012-02-10 12:31

Here is a program that conforms to the current draft of the Mental Midget (MM1X) programming language.

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) then have the program log this information in a file to be accessed later

Name: Anonymous 2012-02-10 12:50

>>34
It's like COBOL and Inform 7, but higher level.
http://www.ifwiki.org/index.php/Inform_7_for_Programmers/Part_1

Name: Anonymous 2012-02-10 13:47

If it ain't Lisp, yer Bwarkers.

Name: Anonymous 2012-02-10 13:48

*dances* Da Land of Lisp

Name: Anonymous 2012-02-10 13:48

Lameda Calcyeless

Name: Anonymous 2012-02-10 13:57

>>35
Yeah reading minds and parsing any human language are definitely high level features.

Name: Anonymous 2012-02-10 14:39

>>21
I took the liberty of adding the file operation:


-module(reddit).
-export([main/0]).

main() ->
    Name = io:get_line("Name: "),
    Age = io:get_line("Age: "),
    Username = io:get_line("Reddit username: "),

    String = io_lib:format("your name is ~s, you are ~s years old, and your username is ~s\n", lists:map(fun(X) -> string:strip(X, right, $\n) end, [Name, Age, Username])),

    io:fwrite(String),
    case file:write_file("dicks", String, [append]) of
        {error, Reason} -> {error_file_operation, Reason};
        _ -> ok
    end.

Name: Anonymous 2012-02-10 14:42

CAWBOL

Name: Anonymous 2012-02-10 14:44

import Control.Applicative ((<$>), (<*>))

prompt :: String -> IO String
prompt str = putStr (str ++ ": ") >> getLine

main :: IO ()
main =
  putStrLn =<<
    (\name age username -> "your name is " ++ name ++ ", you are " ++ age ++ " years old, and your username is " ++ username)
      <$> prompt "Name"
      <*> prompt "Age"
      <*> prompt "Username"

Name: Anonymous 2012-02-10 15:06

Look at all these shitty ugly solutions, Haskell is shit and so are its users.

Name: Anonymous 2012-02-10 15:10

>>43
You're mommas a Monoid of lard.

Name: Anonymous 2012-02-10 15:15

>>44
nice dubs bro

Name: Anonymous 2012-02-10 15:35

IO
The interface between logic and raging monkeys that is always shitting up the purity of our programs.

Name: Anonymous 2012-02-10 15:43

>>46
Programs that have no observable effects can be optimized to a no-op.

Name: Anonymous 2012-02-10 16:04

>>47
Nah, it's just a lazy computation that never gets evaluated.

Name: Anonymous 2012-02-10 18:37

>>47
Most of /gorp/ code could be optimized to main() { return *((int*)0); }.

Name: Anonymous 2012-02-10 21:02

I couldnt think of one
This just shows that scheme sucks

No, it shows that you're a fucking mental midget. Fuck off back to reddit you talentless piece of shit.

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