Sha256: 0e8e6a0a56dd1d94584e0bd1889faa83bba918092ee017800a21704ac8eea5c6

Contents?: true

Size: 1.8 KB

Versions: 138

Compression:

Stored size: 1.8 KB

Contents

(* Merge two ordered lists using the order lt.
 * Pre: the given lists xs and ys must already be ordered per lt.
 * Runs in O(n) time, where n = |xs| + |ys|.
 *)
fun merge lt (xs, ys) =
    let fun loop(out, [], []) = List.rev out
          | loop(out, x::xs, []) = loop (x::out, xs, [])
          | loop(out, [], y::ys) = loop (y::out, [], ys)
          | loop(out, left as x::xs, right as y::ys) =
            if lt (x, y) then loop (x::out, xs, right)
            else loop (y::out, left, ys)
    in
        loop([], xs, ys)
    end

(* mergesort = fn : ('a * 'a -> bool) -> 'a list -> 'a list
 * given an ordering operation and a list of elements that can be ordered
 * returns them in that ordering
 *
 * ex: mergesort (op <) [5,4,3,2,1]
 *     => [1, 2, 3, 4, 5] : int list
 *)
fun mergesort lt xs =
    let val merge' = merge lt
        (* splits a list into two semi-equal halves in Linear time *)
        fun split ns =
            let fun loop([], xs, ys) = (xs, ys)
                  | loop(x::y::zs, xs, ys) = loop(zs, x::xs, y::ys)
                  | loop(x::[], xs, ys) = loop([], x::xs, ys)
            in
                loop(List.rev(ns), [], [])
            end
        fun ms []  = []
          | ms [x] = [x]
          | ms xs = let
              val (l, r) = split xs
          in
              merge'(ms l, ms r)
          end
    in
        ms xs
    end

fun anagrams (candidates, subject) =
  let
    fun toLower s = map Char.toLower (explode s)

    val subject' = toLower subject

    val sort = mergesort (op <)

    fun isAnagram a b = sort a = sort b

    fun collect [] = []
      | collect (s :: ss) =
          if subject' = toLower s
          then collect ss
          else if isAnagram subject' (toLower s)
               then s :: collect ss
               else collect ss
  in
    collect candidates
  end

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.179 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.178 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.177 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.176 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.175 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.174 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.173 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.172 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.171 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.170 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.169 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.167 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.166 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.165 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.164 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.163 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.162 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.161 tracks/sml/exercises/anagram/example.sml
trackler-2.2.1.160 tracks/sml/exercises/anagram/example.sml