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

Pages: 1-

Scheme Output/String Formatting Best Practices

Name: Anonymous 2014-08-28 5:46

What is your preference? Are there other alternatives possible by composing R7RS base library procedures and macros?

; R7RS compliant version
(import (scheme write))
(define world "World)
(for-each display `("Hello, " ,world "!\n")

; R7RS with SRFI 28/29/48 for formatted string templates
(import (scheme write)
(srfi 28))
(define world "World)
(display (format "Hello, ~a!\n" world))

Name: Anonymous 2014-08-28 6:45

R7RS is bloated garbage and I refuse to have anything to do with it.

Name: Anonymous 2014-08-28 14:33

Symta:
World = \World
say "Hello, [World]!"

Name: Anonymous 2014-08-28 14:38

Common Lisp:
(! world = "World"
! "Hello, {world}!")

Name: Anonymous 2014-08-28 19:23

ONE WORD. THE FORCED EXCLAMATIONIFICATION OF THE CODE. THREAD OVER

Name: Anonymous 2014-08-29 1:55

>>4
Is that a reader extension? Looks like shit.

Name: Anonymous 2014-08-29 2:02

>>6

no. it is a defmacro

Name: Anonymous 2014-08-29 2:31

>>2
You are thinking of R6RS. R7RS isn't bloated.

Name: Anonymous 2014-08-29 9:47

I just wrote the following macro and helper procedures for logging text to the terminal and log file, or to an explicit file, for use in my autoconf library.

Portable R7RS doesn't seem to have built in way to append to an output file, so I had to copy the contents of the file first into a list of lines, then I output those lines before appending the new message. Thoughts for improvement?

(define log-file #f) ; this gets initialized later

(define (with-output-to-log-file thunk)
(thunk)
(if (output-port? log-file)
(parameterize ((current-output-port log-file))
(thunk)
(flush-output-port))))

(define (with-output-to-file/append file thunk)
(let ((lines '()))
(if (file-exists? file)
(with-input-from-file file
(lambda ()
(do ((line (read-line) (read-line)))
((eof-object? line))
(set! lines (cons line lines))))))
(with-output-to-file file
(lambda ()
(for-each (lambda (x) (write-string x) (newline)) (reverse lines))
(thunk)))))

(define-syntax ac-echo
(syntax-rules (> >>)
((_ >> file args ...) (with-output-to-file/append file (lambda () (for-each display '(args ...)))))
((_ > file args ...) (with-output-to-file file (lambda () (for-each display '(args ...)))))
((_ args ...) (with-output-to-log-file (lambda () (for-each display '(args ...)))))))

;; Examples

(define you "
/prog/")

(ac-echo "Hello, " you "!\n") ; prints "Hello,
/prog/!" to the terminal and to the current log file if any
(ac-echo > "out.txt" "Hello, " you "!\n") ; writes "Hello,
/prog/!" to the file named "out.txt"
(ac-echo >> "out.txt" "Hello, " you "!\n") ; appends "Hello,
/prog/!" to the file named "out.txt"

Name: Anonymous 2014-08-29 9:48

Formatting was ruined at end. Should be:

(define you "[spoiler]/prog/[/spoiler]")

(ac-echo "Hello, " you "!\n") ; prints "Hello, [spoiler]/prog/[/spoiler]!" to the terminal and to the current log file if any
(ac-echo > "out.txt" "Hello, " you "!\n") ; writes "Hello, [spoiler]/prog/[/spoiler]!" to the file named "out.txt"
(ac-echo >> "out.txt" "Hello, " you "!\n") ; appends "Hello, [spoiler]/prog/[/spoiler]!" to the file named "out.txt"

Name: Anonymous 2014-08-29 21:49

Get this through those thick skulls of yours: THERE ARE NO BEST PRACTICES!!! There are only unsuitable ones, good ones, and better ones for the particular situation at hand, and being able to consider the tradeoffs between all possible solutions is the skill that any engineering is about. Attempting to distill the decisionmaking process of a whole discipline into a set of "best practices" to be followed religiously and brainlessly like trained monkeys is pure idiocy.

Name: Anonymous 2014-08-31 4:22

>>11
Thanks for the advice. And nice dubs.

Name: Anonymous 2014-09-02 0:20

EXPERT PRINT

(import (scheme base)
(scheme process-context)
(scheme write))

(define (terminal-port? port) #t)

(define *color-terminal-supported*
(let-syntax ((color-term? (syntax-rules () ((_ t names ...) (or (string=? t names) ...)))))
(let ((term (get-environment-variable "TERM")))
(if (string? term)
(color-term? term "xterm" "xterm-16color" "xterm-88color" "xterm-256color"
"rxvt" "rxvt-16color" "konsole" "konsole-16color")
#f))))

(define *color-text-enabled* #t)

(define (color-terminal-supported?)
*color-terminal-supported*)

(define (color-terminal-port? port)
(and *color-text-enabled*
*color-terminal-supported*
(terminal-port? port)))

(define (use-color-text?)
*color-text-enabled*)

(define (use-color-text value)
(set! *color-text-enabled* value))

(define-record-type <ansi-tty-code>
(make-ansi-tty-code code)
ansi-tty-code?
(code ansi-tty-code->string))

(define (print-ansi-tty-tokens port-or-ports tokens)
(let loop ((ports port-or-ports))
(unless (null? ports)
(let ((p (if (list? ports) (car ports) ports)))
(cond
((color-terminal-port? p)
(for-each
(lambda (t)
(if (ansi-tty-code? t)
(write-string (ansi-tty-code->string t) p)
(display t p)))
tokens)
(write-string "\x1B[0m" p))
(else
(for-each
(lambda (t)
(unless (ansi-tty-code? t)
(display t p)))
tokens))))
(when (list? port-or-ports)
(loop (cdr port-or-ports))))))

(define-syntax make-ansi-tty-command-token
(syntax-rules ()
((_ commands ...)
(begin (make-ansi-tty-code (string-append "\x1B[0" commands ... "m"))))))

(define-syntax define-print-syntax
(syntax-rules ()
((_ (name print-tokens) ((make-command-token) (command-name command-data) ...))
(begin
(define-syntax command-transformer
(syntax-rules (command-name ...)
((command-transformer ports (tokens (... ...)) ())
(print-tokens ports `(,tokens (... ...))))
((command-transformer ports (tokens (... ...)) (commands (... ...)) command-name expr (... ...))
(command-transformer ports (tokens (... ...)) (commands (... ...) command-data) expr (... ...))) ...
((command-transformer ports (tokens (... ...)) () x expr (... ...))
(command-transformer ports (tokens (... ...) x) () expr (... ...)))
((command-transformer ports (tokens (... ...)) (commands (... ...)) x expr (... ...))
(command-transformer ports (tokens (... ...) (make-command-token commands (... ...))) () x expr (... ...)))))
(define-syntax name
(syntax-rules ()
((name ports expressions (... ...))
(command-transformer ports () () expressions (... ...)))))))))

(define-print-syntax (print-ansi-tty print-ansi-tty-tokens)
((make-ansi-tty-command-token)
(:normal ";0")
(:bold ";1")
(:faint ";2")
(:italic ";3")
(:underline ";4")
(:blink ";5")
(:strikethrough ";9")
(:black ";30")
(:red ";31")
(:green ";32")
(:yellow ";33")
(:blue ";34")
(:magenta ";35")
(:cyan ";36")
(:white ";37")
(:black-bg ";40")
(:red-bg ";41")
(:green-bg ";42")
(:yellow-bg ";43")
(:blue-bg ";44")
(:magenta-bg ";45")
(:cyan-bg ";46")
(:white-bg ";47")))

(define (expert-print text)
(let ((port (current-output-port)))
(let loop ((i 0) (n (string-length text)))
(unless (>= i n)
(print-ansi-tty port :bold :underline :magenta " " :normal " ")
(loop (+ i 2) n)))
(newline port)
(let loop ((i 0) (n (string-length text)))
(unless (>= i n)
(if (= (modulo i 2) 0)
(print-ansi-tty port :bold :italic :magenta (string-ref text i))
(print-ansi-tty port :bold :italic :underline :cyan (string-ref text i)))
(loop (+ i 1) n)))
(newline port)))

(expert-print "EXPERT PROGRAMMER")
(newline)


NO EXCEPTIONS!

Name: Anonymous 2014-09-02 0:44

>>13
mmmmmm~ I am sure that with such a great code you have a really nice anus!

Name: Anonymous 2014-09-02 1:12

>>14
go back to /lgbt/

Name: Anonymous 2014-09-02 1:15

>>15
/lgbt/
And you back to 4chan :)

Name: Anonymous 2014-09-02 5:35

>>13
Defining a macro and using it only once eh? You're having too much fun.

Name: Anonymous 2014-09-02 10:37

>>14

Mine is better!

Name: Anonymous 2014-09-06 6:32

>>17
Macros have other uses too, such as reducing code-duplication, increasing expressivity--even in one-off instances.

Name: Anonymous 2014-09-06 6:58

>>19
even in one-off instances.
This is the reason LITHP is shit - languages extended with arbitratry, unstandardized shit are unreadable by anyone except their author.

Name: Anonymous 2014-09-06 7:02

>>20
unreadable by anyone except their author.
Nobody cares about anyone.

http://www.paulgraham.com/avg.html

Name: Anonymous 2014-09-06 7:08

Lisp is a genuinely good (as in superior) language and this ``LITHP'' idiot is just a troll and new. It and C really are the best of bunch.

Name: Anonymous 2014-09-06 7:09

best of the bunch*

Name: Anonymous 2014-09-06 7:47

>>21
That's the whole point. Programming is a social activity and LITHPers are forever bound to be uncommunicative loners comforting themselves with the "I'm lone wolf genius above the averages" myth.

>>22
It and C really are the best of bunch.
You're clearly either an idiot or a troll. Though the dubs are nice.

Name: Anonymous 2014-09-06 10:14

Also Paul Graham's site died along with his "Arc" crap.

Name: Anonymous 2014-09-06 18:12

>>25

Leah Culver milked the last money out of him. He is now too busy writing Java himself for a hobby project.

Name: Anonymous 2014-09-06 20:07

BTW, why there is no real code in Leah's repo, besides forks and social crap?
https://github.com/leah/getwelltermie

Name: Anonymous 2014-09-06 20:25

I don't understand this leah maymay. Is it because of her funny name?

Name: Anonymous 2014-09-06 22:35

>>28

I just thought I’d introduce myself. My name is Jessica Hartwell. I am a business major at a rather good university. I was introduced to Linux a couple years ago by my boyfriend (a computer science major) and I just love it. It not only looks pretty, but it’s so much more functional than Windows. My boyfriend is currently teaching me C, so I will be posting code snippets from my projects in this blog. I hope to one day contribute to the Ubuntu community that has given us so much to look forward to. :)

Name: Anonymous 2014-09-06 23:37

>>29
My name is Jenny. I am 16 years old and have dark blonde hair. NOW THAT YOU HAVE STARTED READING YOU MAY NOT STOP!! I was murdered July 14th with my fathers shotgun and butcher knife. If you do not post this on 20 other threads i will come to

your house in the middle of the night and kill you with my fathers shotgun and butcher knife. You have 5 hours to complete this task. Dont believe me, *maria marshall, Pelham, Texas 1998, was showering and went to bed right after, found dead the next morning. * keisha jones, Nashville, Tennesee 1995, fell asleep while watching television and mother heard gunshot and scream, found next morning lying on the floor. omar wilkionsin, milwaukee, wisconsin 2002, reading a book to go to bed and was shot and stabbed through the book after he fell asleep. Still dont believe me? google their names....Trust me i did not want to paste this. But its kinda scary so i did it just to be sure

Name: Anonymous 2014-09-07 0:17

>>30

Will you give me pics of your butthole if I do it?

Name: Anonymous 2014-09-07 0:19

>>30

i am a heron. i haev a long neck and i pick fish out of the water w/ my beak. if you dont repost this comment on 10 other pages i will fly into your kitchen tonight and make a mess of your pots and pans

Name: Anonymous 2014-09-07 0:29

I just thought I’d introduce myself. My name is Jessica Hartwell. I am a business major at a rather good university. I was introduced to uncircumcised peni a couple years ago by my boyfriend (an uncircumcised male) and I just love it. It not only looks pretty, but it’s so much more functional than circumcised peni. My boyfriend is currently teaching me blow jobs, so I will be posting selfies from my cock suckings in this blog. I hope to one day contribute to the uncircumcised community that has given us so much to look forward to. :)

Name: Anonymous 2014-09-07 1:02

I just thought I’d introduce myself. My name is Jessica Hartwell. I am a business major at a rather good university. I was introduced to circumcised cock a couple years ago by my boyfriend (a circumcised male) and I just love it. It not only looks better, but it’s so much more functional than uncircumcised cock. My boyfriend is currently teaching me blow jobs, so I will be posting selfies from my dick suckings in this blog. I hope to one day contribute to the circumcised community that has given us so much to look forward to. :D

Name: Anonymous 2014-09-07 1:50

>>34

Shalom!

Name: Anonymous 2014-09-07 6:58

i am a heroin. i'm in a plastic bag and i shoot into veins of the blood w/ a needle. if you dont repost this comment on 10 other pages i will fly into your syringe tonight and give you a really good time

Name: Anonymous 2014-09-07 13:45

>>36
Heroin comes in balloons. Remember how that whore in Pulp Fiction stole Vincent's junk thinking it was coke and overdosed? That's because the dealer put it in a baggy because he was out of balloons when he sold it to him.

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