[Haskell-cafe] request for code review

Neil Mitchell ndmitchell at gmail.com
Sun Mar 5 06:15:07 EST 2006


Hi,

You seem to like let a lot, whereas I hardly ever use them. In general
I find where a lot more readable.

(disclaimer, all notes untested, may not compile, may be wrong)

Also, most haskell programs use $ instead of |>

> -- For convenience:
> currTokType :: ParseContext -> TokenType
> currTokType ctx = ctx |> currTok |> tokenType

this could be written as:
tokenType $ currTok $ ctx

or even more succinctly, using points free style as:
> currTokType = tokenType . currTok

> writeOutput s = \ctx ->
>   let newOutput = s : (output ctx) in
>     ctx {output=newOutput}

why not?
> writeOutput s ctx = ctx{output = s : output ctx}

shorter, no lambda (the argument is moved), no where,  less code more
obvious, and also some redundant brackets have been removed.

> stackTop ctx =
>   let (x:xs) = stack ctx in x
stackTop ctx = head ctx


> pop ctx =
>   let (x:xs) = stack ctx in ctx {stack=xs}

ctx{stack = tail (stack ctx)}


> classifyString s@(c:[])
>   | not (isAlphaNum c)  = Token (Symbol c) s

classifyString s@[c]
means the same thing (a one element list)

> classifyString s        = Token (whichType s) s
>   where whichType "volatile" = Qualifier

isType "volatile" = Qualifier
isType x | x `elem` ["void","char","signed" ...] = Type
isType x = Identifier

-- getting a bit bored here, so less suggestions from now on :)
-- but if you send off a second draft i'll take another look
-- i guess most things are done consistently

>         if ctx |> currTokValue |> (!! 0) |> isDigit
(!! 0) is head

Biggest thing is |> is not a standard Haskell pattern, its usually
either ($) or (.). Also where is great, and case statements are
usually used less.

Thanks

Neil


More information about the Haskell-Cafe mailing list