Skip to content

Instantly share code, notes, and snippets.

@tiye
Created August 8, 2012 11:03
Show Gist options
  • Select an option

  • Save tiye/3294284 to your computer and use it in GitHub Desktop.

Select an option

Save tiye/3294284 to your computer and use it in GitHub Desktop.
sheme with less brackets
#!/usr/bin/coffee
n = (str) -> (str.match /^\s*/)[0].length
add_left = (str, x) ->
x /= 2
left = n str
[1..x].forEach ->
str = str[...left] + '(' + str[left..]
str
add_right = (str, x) ->
x /= 2
[1..x].forEach -> str = str + ')'
str
convert = (str) ->
lines = str.split '\n'
for item, index in lines
n9 = if lines[index-1]? then n lines[index-1] else 0
n0 = n lines[index]
n1 = if lines[index+1]? then n lines[index+1] else 0
if n1 > n0
lines[index] = add_left lines[index], (n1 - n0)
if n0 > n1
lines[index] = add_right lines[index], (n0 - n1)
unless n1 > n0
unless lines[index].trim().length is 0
unless lines[index].trimLeft()[0] is ';'
unless lines[index].trimLeft().indexOf(' ') is -1
lines[index] = add_left lines[index], 1
lines[index] = add_right lines[index], 1
lines.join '\n'
fs = require 'fs'
name = process.argv[2]
nam = name[...-3]
e = name[-3..]
if e isnt '.sm' then throw new Error 'should be .sm file'
namf = nam + '.scm'
{exec} = require 'child_process'
colors = require 'colors'
do run = ->
file = fs.readFileSync name, 'utf8'
file = convert file
fs.writeFileSync namf, file, 'utf8'
console.log "runing #{namf}...".red
exec "guile #{namf}", (err, stdout, stderr) ->
if err? then throw err else
console.log '>>>'.blue, stdout
console.log '--------------'.blue
console.log stderr.red
fs.watchFile name, interval: 1000, run
(define (square x) (* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(display (sqrt 9))
define (square x) (* x x)
define (average x y)
/ (+ x y) 2
define (improve guess x)
average guess (/ x guess)
define (good-enough? guess x)
< (abs (- (square guess) x)) 0.001
define (sqrt-iter guess x)
if (good-enough? guess x)
guess
sqrt-iter (improve guess x)
x
define (sqrt x)
sqrt-iter 1.0 x
display (sqrt 9)

put guil command at /usr/local/bin/. a colors npm module is required to be colorful.

➤➤ guil me.sm 
runing me.scm...
>>> 3.00009155413138
--------------
;;; note: source file /home/chen/coding/guile/is/me.scm
;;;       newer than compiled /home/chen/.cache/guile/ccache/2.0-LE-4-2.0/home/chen/coding/guile/is/me.scm.go
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /home/chen/coding/guile/is/me.scm
;;; compiled /home/chen/.cache/guile/ccache/2.0-LE-4-2.0/home/chen/coding/guile/is/me.scm.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment