Advertisement
Guest User

Untitled

a guest
May 9th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.23 KB | None | 0 0
  1. let Words = ["4815"; "ucwie"; "test"; "sts"; "third"; "1"; "4815"; "ucwie"; "word"; "a"]
  2.  
  3. type lineElement =
  4. |Word of string
  5. |Space of int
  6.  
  7. let rec lineCreator' linesList list length line free =
  8.    match list with
  9.    |hd::tl ->
  10.        let element = Word(hd)
  11.  
  12.        if (String.length hd) <= free then
  13.            let line' = line @ [element]
  14.             match tl with
  15.             |next::nextList ->
  16.                 if String.length next + 1 <= free - String.length hd then
  17.                     let line'' = line' @ [Space(1)]
  18.                    let NowFree = free - String.length hd - 1
  19.                    lineCreator' linesList tl length line'' NowFree
  20.                 else
  21.                     lineCreator' linesList tl length line' 0
  22.             |_ ->
  23.                 lineCreator' linesList tl length line' 0
  24.  
  25.  
  26.         else
  27.             let linesList' = linesList @ [line]
  28.            lineCreator' linesList' list length [] length
  29.    |_->
  30.        let linesList' = linesList @ [line]
  31.         linesList'
  32.  
  33. //отличная идея: чтобы избавиться от лишнего пробела в конце строки, перевернем ее, откусим голову,
  34. //перевернем снова! (рука не поднялась реализовывать)
  35.    
  36. let lineCreator listWords length = lineCreator' [] listWords length [] length //вместо обертки
  37.  
  38. let abc = lineCreator Words 10 //пока работает только первая функция
  39.  
  40. //пробуем печатать
  41.  
  42. let rec spacePrint times =
  43.     if times <= 0 then
  44.         ()
  45.     else
  46.         printf " "
  47.         spacePrint (times - 1)
  48.  
  49. let rec matchingLine line =
  50.     match line with
  51.     |element::lineTail ->
  52.         match element with
  53.         |Space(k) ->
  54.             spacePrint k
  55.             matchingLine lineTail
  56.         |Word(k) ->
  57.             printf "%s" k
  58.             matchingLine lineTail
  59.         |_ -> ()
  60.  
  61.     |_-> ()
  62.  
  63. let rec printLines linesList =
  64.     match linesList with
  65.     |line::tl ->
  66.         matchingLine line
  67.         printfn "."
  68.         printLines tl
  69.     |_ -> ()
  70.  
  71. printLines abc
  72.  
  73. open System
  74. printfn "(Press any key to continue)"
  75. ignore (Console.ReadKey(true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement