Courses/CS 460/Fall 2005/Homework/Josh Cain/Oct 8

From CSWiki

Jump to: navigation, search

[edit] Oct 8 Homework, Josh Cain

Drop

local Drop TestList in
   proc {Drop In Num ?Out}
      case In of Head|Tail then
         if Num > 0 then 
            Out = {Drop Tail Num - 1}
         else
            Out = Head|{Drop Tail 0}
         end
      else
         Out = nil
      end
   end
   TestList = [1 2 3 4 5]
   {Browse {Drop TestList 2}}
end

Filter *Presented in Class

local Filter TestList in
   proc {Filter In Func ?Out}
      case In of Head|Tail then
         if {Func Head} == true then
            Out = Head|{Filter Tail Func}
         else
            Out = {Filter Tail Func}
         end
      else
         Out = nil
      end
   end
   TestList = [2 3 3 4 6 8]
   {Browse {Filter TestList IsOdd}}
end

Flatten

local Flatten TestList in
   proc {Flatten In ?Out}
      case In of Head|Tail then
         case Head of Head2|Tail2 then
            Out = {Flatten Head}
         else
            Out = Head|{Flatten Tail}
         end
      else
         Out = In
      end
   end
   TestList = [1 [1 [1 2 3 4 5 6 7] 2]]
   {Browse {Flatten TestList}}
end

Map

local Map TestList in
   proc {Map In Func ?Out}
      case In of Head|Tail then
         Out = {Func Head}|{Map Tail Func}
      else
         Out = nil
      end
   end
   TestList = [1 2 3]
   {Browse {Map TestList IntToFloat}}
end

Reverse

local Reverse TestList in
   proc {Reverse In ?Out}
      case In of Head|Tail then
         Out = {Reverse Tail}|Head
      else
         Out = nil
      end
   end
   TestList = [1 2 3 4 5]
   {Browse {Reverse TestList}}
end

Non-Recursive MergeSort

local ConvertToSingleton Merge MergeSort TestList in
    proc {ConvertToSingleton In ?Out}
        case In of Head|Tail then
            Out = [Head]|{ConvertToSingleton Tail}
        else
           Out = nil
        end
    end
    proc {Merge List1 List2 ?Out}
       if List1 == nil then
          Out = List2
       elseif List2 == nil then
          Out = List1
       else
          case List1 of Head1|Tail1 then
             case List2 of Head2|Tail2 then
                if Head1 =< Head2 then
                   Out = Head1|{Merge Tail1 List2}
                elseif Head2 > Head1 then
                   Out = Head2|{Merge List1 Tail2}
                end
             end
         end
        end
      end
    proc {MergeSort In ?Out}
       if {Length In} =< 1 then
          Out = In
       else
          local TempList in
             case In of L1|Tmp then
                case Tmp of L2|Tail then
                   {Merge L1 L2 TempList}
                   Out = {MergeSort {Append Tail [TempList]}}
                end
             end
          end
      end
    end
    TestList = [a d c b e z z y t m a y u p]
    {Browse {MergeSort {ConvertToSingleton TestList}}}
end
Personal tools