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

Version Path
trackler-2.0.3.2 tracks/sml/exercises/allergies/example.sml
trackler-2.0.3.1 tracks/sml/exercises/allergies/example.sml
trackler-2.0.3.0 tracks/sml/exercises/allergies/example.sml
trackler-2.0.2.0 tracks/sml/exercises/allergies/example.sml
trackler-2.0.1.2 tracks/sml/exercises/allergies/example.sml
trackler-2.0.1.1 tracks/sml/exercises/allergies/example.sml
trackler-2.0.1.0 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.10 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.9 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.8 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.7 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.6 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.5 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.4 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.3 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.2 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.1 tracks/sml/exercises/allergies/example.sml
trackler-2.0.0.0 tracks/sml/allergies/example.sml
trackler-1.0.4.1 tracks/sml/allergies/example.sml
trackler-1.0.4.0 tracks/sml/allergies/example.sml