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
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>
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."
;; 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)$
>>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.
(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:
Anonymous2012-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);
}
(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.")
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());
}
>>4
Good sir inclusive or madam, you have been trolled.
Name:
Anonymous2012-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> _
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:
Anonymous2012-02-10 3:33
>>18
Read up on the POSIX-2008 scanf m flag. It will crash on Windows though.
Name:
Anonymous2012-02-10 3:41
>>15
Good god read about pointers and memory management in C, sir.
Name:
Anonymous2012-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
>>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.
>>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
>>4
This is ridiculously long and over-complicated.
Functional is shit.
Name:
Anonymous2012-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
>>35
Yeah reading minds and parsing any human language are definitely high level features.
Name:
Anonymous2012-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.
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:
Anonymous2012-02-10 15:06
Look at all these shitty ugly solutions, Haskell is shit and so are its users.
>>47
Nah, it's just a lazy computation that never gets evaluated.
Name:
Anonymous2012-02-10 18:37
>>47
Most of /gorp/ code could be optimized to main() { return *((int*)0); }.
Name:
Anonymous2012-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.