首页 > 其他 > 详细

第一个racket程序:获得字符串中的数字

时间:2014-05-07 03:16:09      阅读:528      评论:0      收藏:0      [点我收藏+]
#! /usr/bin/env racket
#lang racket
#|
  NAME:
     getModelNumber.rkt
     This program is used to get model number based on the
     input model file name.
  USAGE:
     ./getModelNumber.rkt modelfilename
  EXAMPLE:
     ./getModelNumber.rkt gitst22.mat
  OUTPUT:
     22
|#

;; deal with the arguments
(define verbose? (make-parameter #f))
(define fileformat? (make-parameter #f))
(define argslist 
  (command-line
   #:usage-help
   "Get model number based on the filename."
   #:once-each        
   [("-v" "--verbose") "Verbose mode" (verbose? #t)]
   #:args
   (filename [format ".mat"]) (list filename format)))

;; define indicator
(define number -1)
(define filename (list-ref argslist 0))
(define format (list-ref argslist 1))

;; check the filename format is legal.
(if (regexp-match? (string-append format "$") filename)
    (fileformat? #t)
    (when (verbose?)
      (display "Filename must be end format of .mat \n")));

;; get the model number
(define getNumber
  (lambda (filename)
    (regexp-match #px"\\d+" filename)))

(when (and (getNumber filename)
           (fileformat?))
  (set! number
        (string->number (list-ref (getNumber filename) 0))))

number    ; return number value.

运行如下:

bubuko.com,布布扣

第一个racket程序:获得字符串中的数字,布布扣,bubuko.com

第一个racket程序:获得字符串中的数字

原文:http://blog.csdn.net/loveaborn/article/details/25037997

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!