fsharp函数不返回任何内容

时间:2015-02-13 06:08:45

标签: recursion f#

我有以下较小的标记化器用于简单的算术表达式。我是fsharp的新手,我不知道为什么这个函数在被调用时不会返回任何内容。有人可以帮忙吗?

let tokenizer s = 
  let chars1 = scan s
  let rec repeat list = 
    match list with
    | []->[]
    | char::chars ->
      match char with
      | ')' -> RP::repeat chars
      | '(' -> LP::repeat chars
      | '+' -> Plus::repeat chars
      | '*' -> Times::repeat chars
      | '^' -> Pow::repeat chars
      | _ -> 
        let (x,y) = makeInt (toInt char) chars
        Int x::repeat chars
  repeat chars1

1 个答案:

答案 0 :(得分:3)

没有提供scantoIntmakeInt的实现以及表达式的联合类型,但可能会推断为:

let scan (s:string) = s.ToCharArray() |> Array.toList
let toInt c = int c - int '0'
let makeInt n chars = (n,chars)

type expr = RP | LP | Plus | Times | Pow | Int of int

let tokenizer s = 
  let chars1 = scan s
  let rec repeat list = 
    match list with
    | []->[]
    | char::chars ->
      match char with
      | ')' -> RP::repeat chars
      | '(' -> LP::repeat chars
      | '+' -> Plus::repeat chars
      | '*' -> Times::repeat chars
      | '^' -> Pow::repeat chars
      | _ -> 
        let (x,y) = makeInt (toInt char) chars
        Int x::repeat chars
  repeat chars1

在这种情况下:

tokenizer "1+1"

给出:

val it : expr list = [Int 1; Plus; Int 1]

问题可能在于扫描功能的实现。