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.1.0.53 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.52 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.51 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.50 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.49 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.48 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.47 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.46 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.45 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.44 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.43 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.42 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.41 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.40 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.39 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.38 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.37 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.36 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.34 tracks/sml/exercises/allergies/example.sml
trackler-2.1.0.33 tracks/sml/exercises/allergies/example.sml