Sha256: 96be8b7bf873310f9bccc9d5b303200f7e400e723a9a53206f4d97b2e52f2d1e
Contents?: true
Size: 1.58 KB
Versions: 208
Compression:
Stored size: 1.58 KB
Contents
datatype allergen = Eggs | Peanuts | Shellfish | Strawberries | Tomatoes | Chocolate | Pollen | Cats (* valueOf = fn: allergen -> int Given an allergen returns which power of 2 it represents e.g. Eggs => 0 means that Eggs = 2^0 = 1 *) fun valueOf a = case a of Eggs => 0 | Peanuts => 1 | Shellfish => 2 | Strawberries => 3 | Tomatoes => 4 | Chocolate => 5 | Pollen => 6 | Cats => 7 (* isAllergicTo = fn : int -> allergen -> bool Given a code (integer) and an allergen returns true if the person represented by the code is allergic to the specified allergen *) fun isAllergicTo code a = let fun testAllergy code n = let val codeWord = Word.fromInt(code) val nWord = Word.fromInt(n) val oneWord = Word.fromInt(1) in (* ((code >> n) & 1) == 1 *) oneWord = Word.andb( Word.>>(codeWord, nWord), oneWord ) end in testAllergy code (valueOf a) end (* allergies = fn: int -> allergen list Given a code that represents a person's allergies return a list of all allergens that they are allergic to *) fun allergies code = List.filter (isAllergicTo code) [Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats]
Version data entries
208 entries across 208 versions & 1 rubygems