-
-
Save yujiorama/222977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env gosh | |
;; -*- mode: scheme; coding: utf-8 -*- | |
;; seq [OPTION]... FIRST INCREMENT LAST | |
(use gauche.parseopt) | |
(define (usage) | |
(progn | |
(map | |
(lambda (x) (display x) (newline)) | |
'("seq(1) seq(1)" | |
"名前" | |
" seq - 単調増加 (減少) する数値列を表示する" | |
"書式" | |
" seq [-w] [-f FORMAT] [-s SEP] [--equal-width] [--format=FORMAT] [--separator=SEP] <LAST | FIRST LAST | FIRST INCR LAST>" | |
" seq [--help] [--version]" | |
"説明" | |
" seq は FIRST から LAST まで、INCR ずつ加えた数値を表示する。 LAST または INCR が省略された場合、デフォルトは 1 になる。すべての数" | |
" 値は浮動小数点数として解釈される。" | |
"オプション" | |
" -f FORMAT, --format=FORMAT" | |
" f3 のフォーマットに FORMAT を用いる。デフォルト: %g" | |
" -s SEP, --separator=SEP" | |
" 数値を SEP で区切る。デフォルト: ‘\\n’" | |
" -w, --equal-width" | |
" 桁数を揃えるために 先頭を 0 で埋める。" | |
" --help 使用法のメッセージを標準出力に表示し、正常終了する。" | |
" --version" | |
" バージョン情報を標準出力に表示し、正常終了する。")) | |
(exit 0) | |
)) | |
(define (get-first l) | |
(let ((len (length l))) | |
(case len | |
((1) 1) | |
(else (x->number (car l)))))) | |
(define (get-incr l) | |
(if (= (length l) 3) | |
(x->number (cadr l)) | |
1)) | |
(define (get-last l) | |
(let ((len (length l))) | |
(case len | |
((1) (x->number (car l))) | |
((2) (x->number (cadr l))) | |
((3) (x->number (caddr l)))))) | |
(define (print l) | |
(map (lambda (x) (display x)) l)) | |
(define (join s l) | |
(if (pair? l) | |
(cons (car l) | |
(cons s (join s (cdr l)))) | |
(cons l '()))) | |
(define filter map) | |
(define (padding w x) | |
(if (> w (string-length x)) | |
(padding w (string-append "0" x)) | |
x)) | |
(define (seq first incr last) | |
(if (>= first last) | |
last | |
(cons first (seq (+ first incr) incr last)))) | |
(define (main args) | |
(let-args (cdr args) | |
((#f "c") ;; ignore for historical reason | |
(width "w=i" 0) | |
(fmt "f=s" "~d") | |
(separator "s=s" "\n") | |
(#f "h|help" => usage) | |
(else (opt . _) (print "Unknown option : " opt) (usage)) | |
. args) | |
(let ((first (get-first args)) | |
(incr (get-incr args)) | |
(last (get-last args))) | |
(join | |
separator | |
(filter | |
(lambda (x) | |
(padding width x)) | |
(filter | |
(lambda (x) | |
(format fmt x)) | |
(seq first incr last) | |
)))) | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment