Permalink
Browse files

More built in functions

darcs-hash:20060723124355-974a0-f81be8fe3f7a18f809e968b97ae1cac977c67f94.gz
  • Loading branch information...
1 parent 7da3f0a commit 7768de6992f53f50c4b919dbdd2424de8f303472 eb committed Jul 23, 2006
Showing with 82 additions and 5 deletions.
  1. +42 −0 evm/stdfuns.c
  2. +10 −1 evm/stdfuns.h
  3. +6 −4 examples/bigint.e
  4. +24 −0 lib/Prelude.e
View
@@ -32,6 +32,11 @@ char* intToStr(int x)
return buf;
}
+int strIndex(char* str, int i)
+{
+ return (int)(str[i]);
+}
+
char* append(char* x, char* y) {
char* buf = EMALLOC((strlen(x)+strlen(y))*sizeof(char));
strcpy(buf,x);
@@ -67,3 +72,40 @@ int eqBigInt(mpz_t x, mpz_t y) {
return mpz_cmp(x,y)==0;
}
+int ltBigInt(mpz_t x, mpz_t y)
+{
+ return mpz_cmp(x,y)<0;
+}
+
+int gtBigInt(mpz_t x, mpz_t y)
+{
+ return mpz_cmp(x,y)>0;
+}
+
+int leBigInt(mpz_t x, mpz_t y)
+{
+ return mpz_cmp(x,y)<=0;
+}
+
+int geBigInt(mpz_t x, mpz_t y)
+{
+ return mpz_cmp(x,y)>=0;
+}
+
+mpz_t* strToBigInt(char* str)
+{
+ mpz_t* answer = EMALLOC(sizeof(mpz_t));
+ mpz_init(*answer);
+ mpz_set_str(*answer, str, 10);
+ return answer;
+}
+
+char* bigIntToStr(mpz_t x)
+{
+ char* str = mpz_get_str(NULL,10,x);
+ char* buf = EMALLOC(strlen(str)+1);
+ strcpy(buf,str);
+ free(str);
+ return buf;
+}
+
View
@@ -17,9 +17,14 @@ char* readStr();
int strToInt(char* str);
char* intToStr(int x);
-int strToBigInt(char* str);
+mpz_t* strToBigInt(char* str);
char* bigIntToStr(mpz_t x);
+// String operations
+
+int strIndex(char* str, int i);
+char* append(char* x, char* y);
+
// Big integer arithmetic
mpz_t* addBigInt(mpz_t x, mpz_t y);
@@ -28,6 +33,10 @@ mpz_t* mulBigInt(mpz_t x, mpz_t y);
mpz_t* divBigInt(mpz_t x, mpz_t y);
int eqBigInt(mpz_t x, mpz_t y);
+int ltBigInt(mpz_t x, mpz_t y);
+int gtBigInt(mpz_t x, mpz_t y);
+int leBigInt(mpz_t x, mpz_t y);
+int geBigInt(mpz_t x, mpz_t y);
#endif
View
@@ -1,7 +1,9 @@
-main () -> Unit = printBig(fact(120L))
+main () -> Unit = putStrLn(bigIntToStr(fact(10000L)))
-fact (x:BigInt) -> BigInt =
+fact (x:BigInt) -> BigInt = factAux(x,1L)
+
+factAux (x:BigInt, acc:BigInt) -> BigInt =
if (eqBig(x,0L))
- then 1L
- else mulBig(x,fact(subBig(x,1L)))
+ then acc
+ else factAux(subBig(x,1L), mulBig(x,acc))
View
@@ -14,6 +14,9 @@ readStr () -> String =
intToStr (x:Int) -> String =
foreign String "intToStr" (x:Int)
+strToInt (x:String) -> Int =
+ foreign String "strToInt" (x:String)
+
printInt (x:Int) -> Unit =
let foo:Unit = foreign Unit "printInt" (x:Int) in unit
@@ -25,6 +28,9 @@ append (x:String, y:String) -> String =
length (x:String) -> String =
foreign Int "strlen" (x:String)
+index (x:String, i:Int) -> Char =
+ foreign Int "strIndex" (x:String, i:Int)
+
-- Big number arithmetic
addBig (x:BigInt, y:BigInt) -> BigInt =
@@ -39,6 +45,24 @@ mulBig (x:BigInt, y:BigInt) -> BigInt =
eqBig (x:BigInt, y:BigInt) -> Bool =
foreign Int "eqBigInt" (x:BigInt, y:BigInt)
+ltBig (x:BigInt, y:BigInt) -> Bool =
+ foreign Int "ltBigInt" (x:BigInt, y:BigInt)
+
+gtBig (x:BigInt, y:BigInt) -> Bool =
+ foreign Int "gtBigInt" (x:BigInt, y:BigInt)
+
+leBig (x:BigInt, y:BigInt) -> Bool =
+ foreign Int "leBigInt" (x:BigInt, y:BigInt)
+
+geBig (x:BigInt, y:BigInt) -> Bool =
+ foreign Int "geBigInt" (x:BigInt, y:BigInt)
+
printBig (x:BigInt) -> Unit =
foreign Unit "printBigInt" (x:BigInt)
+bigIntToStr (x:BigInt) -> String =
+ foreign String "bigIntToStr" (x:BigInt)
+
+strToBigInt (x:String) -> Int =
+ foreign String "strToBigInt" (x:String)
+

0 comments on commit 7768de6

Please sign in to comment.