how can I define functions in haskell using uppercase letters? -
this question has reply here:
how name haskell variables in physics uppercase 3 answersfor example, how can define function generate fibonacci numbers using uppercase letters this: fib n need phone call function terminal using uppercase letters this: fib 7
you can't. functions in haskell must start lowercase unicode characters or underscore. symbols origin upper case letter reserved types , constructors.
why need phone call function terminal using upper case letters? seems rather arbitrary constraint.
if really wanted read stdin , process that, suggest writing this
import system.io import text.read (readmaybe) import control.monad (forever) info operation = fib integer | fac integer | add together integer integer deriving (read) -- read instance of import here fib :: integer -> integer fib 0 = 1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) eval :: operation -> integer eval (fib n) = fib n eval (fac n) = product [1..n] eval (add b) = + b repl :: io () repl = forever loop parseresponse :: string -> string parseresponse resp = case readmaybe resp of op -> show $ eval op nil -> "invalid operation" loop = putstr "> " resp <- getline putstrln $ parseresponse resp main :: io () main = hsetbuffering stdin linebuffering hsetbuffering stdout nobuffering repl
this allows user type in exact name of info constructor, interpret info constructor (yes, entire code simple interactive scripting language can fibs, factorials, , addition) result. if invalid operation typed in, respond invalid operation
. can quit interpreter ctrl-c.
function haskell
No comments:
Post a Comment