This file contains hidden or 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
def rpn(str: String): Int = rpnPrime(str.split(' ').toList, Nil) | |
def rpnPrime(str: List[String], stack: List[Int]): Int = (str, stack) match { | |
case ("+"::t, y::x::zs) => rpnPrime(t, (x + y) :: zs) | |
case ("-"::t, y::x::zs) => rpnPrime(t, (x - y) :: zs) | |
case ("*"::t, y::x::zs) => rpnPrime(t, (x * y) :: zs) | |
case ("/"::t, y::x::zs) => rpnPrime(t, (x / y) :: zs) | |
case ("%"::t, y::x::zs) => rpnPrime(t, (x % y) :: zs) | |
case ( n::t, zs) => rpnPrime(t, (n.toInt) :: zs) | |
case _ => stack.head |
This file contains hidden or 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
import scala.language.implicitConversions | |
sealed trait Expr { | |
def +(that: Expr): Expr = Add(List(this, that)) | |
def *(that: Expr): Expr = Mul(List(this, that)) | |
def +(that: Int ): Expr = this + N( that) | |
def -(that: Int ): Expr = this + N(-that) | |
def *(that: Int ): Expr = this * N( that) |
This file contains hidden or 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
import scala.language.implicitConversions | |
// R is derived from Rational with some fixes. | |
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6 | |
case class R(numer: Int, denom: Int) extends Ordered[R] { | |
def reduce: R = { | |
def gcd(x: Int, y: Int): Int = { | |
if (x == 0) y | |
else if (x < 0) gcd(-x, y) |
This file contains hidden or 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
import scala.language.implicitConversions | |
// R is derived from Rational with some fixes. | |
// https://sites.google.com/site/scalajp/home/documentation/scala-by-example/chapter6 | |
case class R(n: Int, d: Int) extends Ordered[R] { | |
private def gcd(x: Int, y: Int): Int = { | |
if (x == 0) y | |
else if (x < 0) gcd(-x, y) | |
else if (y < 0) -gcd( x, -y) |
This file contains hidden or 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
sealed trait Expr | |
case class N(n: Int) extends Expr | |
case class Var(x: String, a: Int, n: Int) extends Expr | |
case class Add(n: Expr*) extends Expr | |
case class Mul(n: Expr*) extends Expr | |
def x(a: Int, n: Int): Var = { | |
Var("x", a, n) | |
} |
This file contains hidden or 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
sealed trait Expr | |
case class N(n: Int) extends Expr | |
case class Var(x: String, a: Int, n: Int) extends Expr | |
case class Add(n: Expr*) extends Expr | |
case class Mul(n: Expr*) extends Expr | |
def add(xs: List[Expr]): Expr = xs match { | |
case List() => N(0) | |
case List(x) => x | |
case _ => Add(xs.toArray: _*) // Add(WrappedArray(N(1), N(2), Var(x,2,2))) |
This file contains hidden or 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
sealed trait Expr | |
case class N(n: Int) extends Expr | |
case class Add(n: Expr*) extends Expr | |
case class Mul(n: Expr*) extends Expr | |
def str(e: Expr): String = e match { | |
case N(x) => x.toString | |
case Add() => "" | |
case Add(x) => str(x) | |
case Add(x, xs@_*) => str(x) ++ "+" ++ str(Add(xs: _*)) |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct A{ | |
char s[10000]; | |
A() { | |
strcpy(s, "123"); | |
} |
This file contains hidden or 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
void inc(int &x) { | |
++x; | |
} | |
void inc(int *x) { | |
++*x; | |
} |
This file contains hidden or 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
var fs = require("fs"); | |
function convLE(len, v) { | |
var ret = ""; | |
for (var i = 0; i < len; ++i) { | |
ret += String.fromCharCode(v & 0xff); | |
v >>= 8; | |
} | |
return ret; | |
} |
NewerOlder