Last active
November 12, 2019 00:24
-
-
Save ymakino/2379182 to your computer and use it in GitHub Desktop.
Helper functions for daily use in SML.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
signature HELPER = | |
sig | |
val power : int * int -> int | |
val apply : ('a -> 'b) * 'a -> 'b | |
val id : 'a -> 'a | |
val curry : ('a * 'b -> 'c) -> ('a -> 'b -> 'c) | |
val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c) | |
val flip : ('a * 'b -> 'c) -> ('b * 'a -> 'c) | |
val toBin : int -> string | |
val toOct : int -> string | |
val toDec : int -> string | |
val toHex : int -> string | |
val times : ('a -> 'a) -> int -> 'a -> 'a | |
val zip : 'a list * 'b list -> ('a * 'b) list | |
val zipWith : ('a * 'b -> 'c) -> 'a list * 'b list -> 'c list | |
end | |
structure Helper :> HELPER = | |
struct | |
infix 8 ** | |
fun power (n, 0) = 1 | |
| power (n, 1) = n | |
| power (n, m) = | |
let | |
val hp = power (n, m div 2) | |
val r = power(n, m mod 2) | |
in | |
hp * hp * r | |
end | |
fun apply (f, x) = f x | |
fun id x = x | |
fun curry f x y = f (x, y) | |
fun uncurry f (x, y) = f x y | |
fun flip f (x, y) = f (y, x) | |
val toBin = Int.fmt StringCvt.BIN | |
val toOct = Int.fmt StringCvt.OCT | |
val toDec = Int.fmt StringCvt.DEC | |
val toHex = Int.fmt StringCvt.HEX | |
fun times f 0 x = x | |
| times f n x = times f (n - 1) (f x) | |
fun zipWith f ([], _) = [] | |
| zipWith f (_, []) = [] | |
| zipWith f (x::xs, y::ys) = f (x, y) :: zipWith f (xs, ys) | |
fun zip x = zipWith id x | |
end | |
val $ = Helper.apply | |
val ** = Helper.power | |
open Helper | |
infix 2 $ | |
infix 8 ** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment