This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1 @@
Create an enumeration of constants with and without explicit values.

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,22 @@
(defun symbol-to-constant (sym)
(intern (concatenate 'string "*" (symbol-name sym) "*")
"ACL2"))
(defmacro enum-with-vals (symbol value &rest args)
(if (endp args)
`(defconst ,(symbol-to-constant symbol) ,value)
`(progn (defconst ,(symbol-to-constant symbol) ,value)
(enum-with-vals ,@args))))
(defun interleave-with-nats-r (xs i)
(if (endp xs)
nil
(cons (first xs)
(cons i (interleave-with-nats-r (rest xs)
(1+ i))))))
(defun interleave-with-nats (xs)
(interleave-with-nats-r xs 0))
(defmacro enum (&rest symbols)
`(enum-with-vals ,@(interleave-with-nats symbols)))

View file

@ -0,0 +1,13 @@
BEGIN # example 1 #
MODE FRUIT = INT;
FRUIT apple = 1, banana = 2, cherry = 4;
FRUIT x := cherry;
CASE x IN
print(("It is an apple #",x, new line)),
print(("It is a banana #",x, new line)),
SKIP, # 3 not defined #
print(("It is a cherry #",x, new line))
OUT
SKIP # other values #
ESAC
END;

View file

@ -0,0 +1,24 @@
BEGIN # example 2 #
MODE ENUM = [0]CHAR; # something with minimal size #
MODE APPLE = STRUCT(ENUM apple), BANANA = STRUCT(ENUM banana), CHERRY = STRUCT(ENUM cherry);
MODE FRUIT = UNION(APPLE, BANANA, CHERRY);
OP REPR = (FRUIT f)STRING:
CASE f IN
(APPLE):"Apple",
(BANANA):"Banana",
(CHERRY):"Cherry"
OUT
"?" # uninitalised #
ESAC;
FRUIT x := LOC CHERRY;
CASE x IN
(APPLE):print(("It is an ",REPR x, new line)),
(BANANA):print(("It is a ",REPR x, new line)),
(CHERRY):print(("It is a ",REPR x, new line))
OUT
SKIP # uninitialised FRUIT #
ESAC
END

View file

@ -0,0 +1,3 @@
fruit["apple"]=1; fruit["banana"]=2; fruit["cherry"]=3
fruit[1]="apple"; fruit[2]="banana"; fruit[3]="cherry"
i=0; apple=++i; banana=++i; cherry=++i;

View file

@ -0,0 +1,2 @@
type Fruit is (apple, banana, cherry); -- No specification of the representation value;
for Fruit use (apple => 1, banana => 2, cherry => 4); -- specification of the representation values

View file

@ -0,0 +1,7 @@
ENUM APPLE, BANANA, CHERRY
PROC main()
DEF x
ForAll({x}, [APPLE, BANANA, CHERRY],
`WriteF('\d\n', x))
ENDPROC

View file

@ -0,0 +1,3 @@
fruit_%apple% = 0
fruit_%banana% = 1
fruit_%cherry% = 2

View file

@ -0,0 +1,16 @@
REM Impossible. Can only be faked with arrays of strings.
OPTION BASE 1
DIM SHARED fruitsName$(1 to 3)
DIM SHARED fruitsVal%( 1 to 3)
fruitsName$[1] = "apple"
fruitsName$[2] = "banana"
fruitsName$[3] = "cherry"
fruitsVal%[1] = 1
fruitsVal%[2] = 2
fruitsVal%[3] = 3
REM OR GLOBAL CONSTANTS
DIM SHARED apple%, banana%, cherry%
apple% = 1
banana% = 2
cherry% = 3

View file

@ -0,0 +1,3 @@
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };

View file

@ -0,0 +1,3 @@
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };

View file

@ -0,0 +1,11 @@
; a set of keywords
(def fruits #{:apple :banana :cherry})
; a predicate to test "fruit" membership
(defn fruit? [x] (contains? fruits x))
; if you need a value associated with each fruit
(def fruit-value (zipmap fruits (iterate inc 1)))
(println (fruit? :apple))
(println (fruit-value :banana))

View file

@ -0,0 +1,8 @@
;; symbol to number
(defconstant +apple+ 0)
(defconstant +banana+ 1)
(defconstant +cherry+ 2)
;; number to symbol
(defun index-fruit (i)
(aref #(+apple+ +banana+ +cherry+) i))

View file

@ -0,0 +1,2 @@
(deftype fruit ()
'(member +apple+ +banana+ +cherry+))

View file

@ -0,0 +1,17 @@
// Anonymous
enum { APPLE, BANANA, CHERRY }
// Named (commonlu used enum in D) (uint)
enum Fruits1 { apple, banana, cherry }
// Named with specified values (uint)
enum Fruits2 { apple = 0, banana = 10, cherry = 20 }
// Named, typed and with specified values
enum Fruits3 : ubyte { apple = 0, banana = 100, cherry = 200 }
void main() {
static assert(CHERRY == 2);
int f1 = Fruits2.banana; // No error
// Fruits2 f2 = 1; // Compilation error
}

View file

@ -0,0 +1,2 @@
type TFruit = (Apple, Banana, Cherry);
type TApe = (Gorilla = 0, Chimpanzee = 1, Orangutan = 5);

View file

@ -0,0 +1,4 @@
type
TFruit = (Apple, Banana, Cherry);
TApe = (Gorilla = 0, Chimpanzee = 1, Orangutan = 5);

View file

@ -0,0 +1,3 @@
def apple { to value() { return 0 } }
def banana { to value() { return 1 } }
def cherry { to value() { return 2 } }

View file

@ -0,0 +1,6 @@
interface Fruit guards FruitStamp {}
def apple implements FruitStamp {}
def banana implements FruitStamp {}
def cherry implements FruitStamp {}
def eat(fruit :Fruit) { ... }

View file

@ -0,0 +1,4 @@
def [Fruit, [=> apple, => banana, => cherry]] := makeEnumeration()
def [Fruit, [=> apple, => banana, => cherry]] :=
makeEnumeration(0, ["apple", "banana", "cherry"])

View file

@ -0,0 +1,27 @@
// Without explicit values
enumeration FruitsKind
APPLE,
BANANA,
CHERRY
end
program EnumerationTest
function main()
whatFruitAmI(FruitsKind.CHERRY);
end
function whatFruitAmI(fruit FruitsKind)
case (fruit)
when(FruitsKind.APPLE)
syslib.writestdout("You're an apple.");
when(FruitsKind.BANANA)
syslib.writestdout("You're a banana.");
when(FruitsKind.CHERRY)
syslib.writestdout("You're a cherry.");
otherwise
syslib.writestdout("I'm not sure what you are.");
end
end
end

View file

@ -0,0 +1,27 @@
// With explicit values
library FruitsKind type BasicLibrary {}
const APPLE int = 0;
const BANANA int = 1;
const CHERRY int = 2;
end
program EnumerationTest
function main()
whatFruitAmI(FruitsKind.CHERRY);
end
function whatFruitAmI(fruit int in)
case (fruit)
when(FruitsKind.APPLE)
syslib.writestdout("You're an apple.");
when(FruitsKind.BANANA)
syslib.writestdout("You're a banana.");
when(FruitsKind.CHERRY)
syslib.writestdout("You're a cherry.");
otherwise
syslib.writestdout("I'm not sure what you are.");
end
end
end

View file

@ -0,0 +1,4 @@
0 CONSTANT apple
1 CONSTANT banana
2 CONSTANT cherry
...

View file

@ -0,0 +1 @@
: ENUM ( n -<name>- n+1 ) DUP CONSTANT 1+ ;

View file

@ -0,0 +1 @@
0 ENUM APPLE ENUM BANANA ENUM CHERRY DROP

View file

@ -0,0 +1 @@
0 ENUM FIRST ENUM SECOND ... CONSTANT LAST

View file

@ -0,0 +1,2 @@
: SIZED-ENUM ( n s -<name>- n+s ) OVER CONSTANT + ;
: CELL-ENUM ( n -<name>- n+cell ) CELL SIZED-ENUM ;

View file

@ -0,0 +1,6 @@
0 ENUM FIRST \ value = 0
CELL-ENUM SECOND \ value = 1
ENUM THIRD \ value = 5
3 SIZED-ENUM FOURTH \ value = 6
ENUM FIFTH \ value = 9
CONSTANT SIXTH \ value = 10

View file

@ -0,0 +1,4 @@
: CONSTANTS ( n -- ) 0 DO I CONSTANT LOOP ;
\ resistor digit colors
10 CONSTANTS black brown red orange yellow green blue violet gray white

View file

@ -0,0 +1,4 @@
enum, bind(c)
enumerator :: one=1, two, three, four, five
enumerator :: six, seven, nine=9
end enum

View file

@ -0,0 +1,3 @@
enum, bind(c) :: nametype
enumerator :: one=1, two, three
end enum nametype

View file

@ -0,0 +1,5 @@
const (
apple = iota
banana
cherry
)

View file

@ -0,0 +1,5 @@
const (
apple = 0
banana = 1
cherry = 2
)

View file

@ -0,0 +1 @@
data Fruit = Apple | Banana | Cherry deriving Enum

View file

@ -0,0 +1,3 @@
enum Fruits{
APPLE, BANANA, CHERRY
}

View file

@ -0,0 +1,6 @@
enum Fruits{
APPLE(0), BANANA(1), CHERRY(2)
private final int value;
fruits(int value) { this.value = value; }
public int value() { return value; }
}

View file

@ -0,0 +1 @@
var fruits = { apple : 0, banana : 1, cherry : 2 };

View file

@ -0,0 +1 @@
local fruit = {apple = 0, banana = 1, cherry = 2}

View file

@ -0,0 +1 @@
local apple, banana, cherry = 0,1,2

View file

@ -0,0 +1,5 @@
stuff = {'apple', [1 2 3], 'cherry',1+2i}
stuff =
'apple' [1x3 double] 'cherry' [1.000000000000000 + 2.000000000000000i]

View file

@ -0,0 +1,18 @@
// Using an array/hash
$fruits = array( "apple", "banana", "cherry" );
$fruits = array( "apple" => 0, "banana" => 1, "cherry" => 2 );
// If you are inside a class scope
class Fruit {
const APPLE = 0;
const BANANA = 1;
const CHERRY = 2;
}
// Then you can access them as such
$value = Fruit::APPLE;
// Or, you can do it using define()
define("FRUIT_APPLE", 0);
define("FRUIT_BANANA", 1);
define("FRUIT_CHERRY", 2);

View file

@ -0,0 +1,5 @@
# Using an array
my @fruits = qw(apple banana cherry);
# Using a hash
my %fruits = ( apple => 0, banana => 1, cherry => 2 );

View file

@ -0,0 +1,2 @@
(de enum "Args"
(mapc def "Args" (range 1 (length "Args"))) )

View file

@ -0,0 +1,2 @@
: (enum A B C D E F)
-> F

View file

@ -0,0 +1 @@
FIRST_NAME, LAST_NAME, PHONE = range(3)

View file

@ -0,0 +1 @@
vars().update((key,val) for val,key in enumerate(("FIRST_NAME","LAST_NAME","PHONE")))

View file

@ -0,0 +1,3 @@
factor(c("apple", "banana", "cherry"))
# [1] apple banana cherry
# Levels: apple banana cherry

View file

@ -0,0 +1,50 @@
/*REXX program to illustrate enumeration of constants via stemmed arrays*/
fruit.=0 /*the default for all "FRUITS." */
fruit.apple = 65
fruit.cherry = 4
fruit.kiwi = 12
fruit.peach = 48
fruit.plum = 50
fruit.raspberry = 17
fruit.tomato = 8000
fruit.ugli = 2
fruit.watermelon = 0.5 /*could also specify: 1/2 */
/*A partial list of some fruits (below). */
/* [↓] This is one method of using a list. */
FruitList='apple apricot avocado banana bilberry blackberry blackcurrent blueberry baobab boysenberry breadfruit cantalope cherry chilli chokecherry citront',
'coconut cranberry cucumber current date dragonfruit durian eggplant elderberry fig feijoa gac gooseberry grape grapefruit guava honeydew huckleberry jackfruit',
'jambul juneberry kiwi kumquat lemon lime lingenberry loquat lychee mandarine mango mangosteen netarine orange papaya passionfruit peach pear persimmon',
'physalis pineapple pitaya pomegranate pomelo plum pumpkin rambutan raspberry redcurrent satsuma squash strawberry tangerine tomato ugli watermelon zucchini'
/*┌────────────────────────────────────────────────────────────────────┐
Spoiler alert: sex is discussed below: PG-13. Most berries don't
have "berry" in their name. A berry is a simple fruit produced
from a single ovary. Some true berries are: pomegranate, guava,
eggplant, tomato, chilli, pumpkin, cucumber, melon, and citruses.
Blueberry is a false berry, blackberry is an aggregate fruit,
and strawberry is an accessory fruit. Most nuts are fruits.
The following aren't true nuts: almond, cashew, coconut,
macadamia, peanut, pecan, pistachio, and walnut.
*/
/* [↓] due to a Central America blight in 1922.*/
if fruit.banana=0 then say "Yes! We have no bananas today." /*(sic)*/
if fruit.kiwi \=0 then say "We gots" fruit.kiwi "hairy fruit." /*(sic)*/
if fruit.peach\=0 then say "We gots" fruit.peach "fuzzy fruit." /*(sic)*/
maxL = length(' fruit ')
maxQ = length(' quantity ')
say
do pass=1 for 2 /*first pass finds the maximums. */
do j=1 for words(FruitList)
f=word(FruitList,j) /*get a fruit name from the list.*/
q=value('FRUIT.'f)
if pass==1 then do /*widest fruit name and quantity.*/
maxL=max(maxL,length(f)) /*longest fruit name*/
maxQ=max(maxQ,length(q)) /*widest fruit quant*/
iterate /*j*/
end
if j==1 then say center('fruit',maxL) center('quantity',maxQ)
if j==1 then say copies('',maxL) copies('',maxQ)
if q\=0 then say right(f,maxL) right(q,maxQ)
end /*j*/
end /*pass*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,5 @@
module Fruits
APPLE = 0
BANANA = 1
CHERRY = 2
end

View file

@ -0,0 +1,4 @@
sealed abstract class Fruit
case object Apple extends Fruit
case object Banana extends Fruit
case object Cherry extends Fruit

View file

@ -0,0 +1,3 @@
object Fruit extends Enumeration {
val Apple, Banana, Cherry = Value
}

View file

@ -0,0 +1,8 @@
(define apple 0)
(define banana 1)
(define cherry 2)
(define (fruit? atom)
(or (equal? 'apple atom)
(equal? 'banana atom)
(equal? 'cherry atom)))

View file

@ -0,0 +1,4 @@
proc enumerate {name values} {
interp alias {} $name: {} lsearch $values
interp alias {} $name@ {} lindex $values
}

View file

@ -0,0 +1,6 @@
enumerate fruit {apple blueberry cherry date elderberry}
fruit: date
# ==> prints "3"
fruit@ 2
# ==> prints "cherry"