View Category
Find the distance between two points
haskell
data Floating n => Point2 n = Point2 n n
distance :: Floating n => Point2 n -> Point2 n -> n
distance (Point2 x1 y1) (Point2 x2 y2) = sqrt (x'*x' + y'*y')
where
x' = x1 - x2
y' = y1 - y2
-- > distance (Point2 5 10) (Point2 3 5)
-- 5.385...
-- > distance (Point2 1 1) (Point2 2 2)
-- 1.414...
distance :: Floating n => Point2 n -> Point2 n -> n
distance (Point2 x1 y1) (Point2 x2 y2) = sqrt (x'*x' + y'*y')
where
x' = x1 - x2
y' = y1 - y2
-- > distance (Point2 5 10) (Point2 3 5)
-- 5.385...
-- > distance (Point2 1 1) (Point2 2 2)
-- 1.414...
csharp
System.Drawing.Point p = new System.Drawing.Point(13, 14),
p1 = new System.Drawing.Point(10, 10);
double distance = Math.Sqrt(Math.Pow(p1.X - p.X, 2) + Math.Pow(p1.Y - p.Y, 2)));
p1 = new System.Drawing.Point(10, 10);
double distance = Math.Sqrt(Math.Pow(p1.X - p.X, 2) + Math.Pow(p1.Y - p.Y, 2)));
Zero pad a number
Given the number 42, pad it to 8 characters like 00000042
haskell
import Text.Printf
printf "%08d" 42
printf "%08d" 42
csharp
string.Format("{0,8:D8}", 42);
Right Space pad a number
Given the number 1024 right pad it to 6 characters
"1024 "
haskell
let s = show 1024
p = 6
in s ++ (replicate (p - length s) ' ')
p = 6
in s ++ (replicate (p - length s) ' ')
import Text.Printf
main = do
putStrLn $ printf "%-6d" (1024::Int)
main = do
putStrLn $ printf "%-6d" (1024::Int)
csharp
public class NumberRightPadding {
public static void Main() {
string withStringDotFormat = string.Format("{0,-6}", 1024);
string withToStringDotPadRight = 1024.ToString().PadRight(6);
}
}
public static void Main() {
string withStringDotFormat = string.Format("{0,-6}", 1024);
string withToStringDotPadRight = 1024.ToString().PadRight(6);
}
}
Format a decimal number
Format the number 7/8 as a decimal with 2 places: 0.88
haskell
import Text.Printf
printf "%3.2f" (7/8)
printf "%3.2f" (7/8)
main = putStrLn $ Numeric.showFFloat (Just 2) (7/8) ""
csharp
public class FormatDecimal {
public static void Main() {
decimal result = decimal.Round( 7 / 8m, 2);
System.Console.WriteLine(result);
}
}
public static void Main() {
decimal result = decimal.Round( 7 / 8m, 2);
System.Console.WriteLine(result);
}
}
Left Space pad a number
Given the number 73 left pad it to 10 characters
" 73"
haskell
import Text.Printf
formatted :: String
formatted = printf "%10d" 73
formatted :: String
formatted = printf "%10d" 73
csharp
public class NumberLeftPadding {
public static void Main() {
string withStringDotFormat = string.Format("{0,10}", 73);
string withToStringDotPadLeft = 73.ToString().PadLeft(10);
}
}
public static void Main() {
string withStringDotFormat = string.Format("{0,10}", 73);
string withToStringDotPadLeft = 73.ToString().PadLeft(10);
}
}
Generate a random integer in a given range
Produce a random integer between 100 and 200 inclusive
haskell
import System.Random
randInRange :: Int -> Int -> IO Int
randInRange a b = getStdRandom $ randomR (a, b)
main = randInRange 100 200 >>= print
randInRange :: Int -> Int -> IO Int
randInRange a b = getStdRandom $ randomR (a, b)
main = randInRange 100 200 >>= print
import System.Random
main = randomRIO (1,100) >>= print
main = randomRIO (1,100) >>= print
csharp
System.Random r = new System.Random();
int random = r.Next(100,201);
int random = r.Next(100,201);
Generate a repeatable random number sequence
Initialise a random number generator with a seed and generate five decimal values. Reset the seed and produce the same values.
haskell
import System.Random
import Control.Monad (forM_)
main = do
printRands
printRands
where printRands = forM_ [1..5] (\i -> print (randInt i))
randInt i = fst $ randomR (100, 200) (mkStdGen i) :: Int
import Control.Monad (forM_)
main = do
printRands
printRands
where printRands = forM_ [1..5] (\i -> print (randInt i))
randInt i = fst $ randomR (100, 200) (mkStdGen i) :: Int
import System.Random
gen1 = mkStdGen 12345
gen2 = mkStdGen 12345
main = do
print $ take 5 (randoms gen1 :: [Float])
print $ take 5 (randoms gen2 :: [Float])
gen1 = mkStdGen 12345
gen2 = mkStdGen 12345
main = do
print $ take 5 (randoms gen1 :: [Float])
print $ take 5 (randoms gen2 :: [Float])
csharp
using System;
public class RepeatableRandom {
public static void Main() {
var r = new Random(12); // seed is 12
for (int i = 0; i < 5; i++)
Console.WriteLine(r.Next());
r = new Random(12);
for (int i = 0; i < 5; i++)
Console.WriteLine(r.Next());
}
}
public class RepeatableRandom {
public static void Main() {
var r = new Random(12); // seed is 12
for (int i = 0; i < 5; i++)
Console.WriteLine(r.Next());
r = new Random(12);
for (int i = 0; i < 5; i++)
Console.WriteLine(r.Next());
}
}
