Using pattern matching and tail recursion in Elixir:
defmoduleRomando@moduledocfalse@specto_integer(binary())::{:ok,pos_integer()}|:errordefto_integer(number,acc\\0)defto_integer("",acc),do:{:ok,acc}defto_integer(number,acc)dowith{value,rest}<-num(number)doto_integer(rest,acc+value)endend@specnum(binary())::{pos_integer(),binary()}|:errordefpnum(number)defpnum("M"<>rest),do:{1000,rest}defpnum("CM"<>rest),do:{900,rest}defpnum("D"<>rest),do:{500,rest}defpnum("CD"<>rest),do:{400,rest}defpnum("C"<>rest),do:{100,rest}defpnum("XC"<>rest),do:{90,rest}defpnum("L"<>rest),do:{50,rest}defpnum("XL"<>rest),do:{40,rest}defpnum("X"<>rest),do:{10,rest}defpnum("IX"<>rest),do:{9,rest}defpnum("V"<>rest),do:{5,rest}defpnum("IV"<>rest),do:{4,rest}defpnum("I"<>rest),do:{1,rest}defpnum(_),do::errorendsolution=fnx->caseRoman.to_integer(x)do{:ok,value}->value:error->raiseArgumentError,message:"Wrong roman numeral format"endend
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Using pattern matching and tail recursion in Elixir: