Advertisement
Guest User

Untitled

a guest
May 9th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.78 KB | None | 0 0
  1. let Words = ["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.        if String.length hd + 1 <= free then
  12.            let line' = line @ [element]
  13.             let line'' = line' @ [Space(1)]
  14.            let NowFree = free - String.length hd - 1
  15.            lineCreator' lineslist tl length line'' NowFree
  16.         else
  17.             let lineslist' = lineslist @ [line]
  18.            let line' = [element]
  19.             let line'' = line' @ [Space(1)]
  20.            let NowFree = length - String.length hd - 1
  21.            lineCreator' lineslist' tl length line'' NowFree
  22.    |_-> lineslist
  23.  
  24. //отличная идея: чтобы избавиться от лишнего пробела в конце строки, перевернем ее, откусим голову,
  25. //перевернем снова! (рука не поднялась реализовывать)
  26.    
  27. let lineCreator listWords length = lineCreator' [] listWords length [] length //вместо обертки
  28.  
  29. let abc = lineCreator Words 6 //пока работает только первая функция
  30.  
  31. //пробуем печатать
  32.  
  33. let rec matchingLine line =
  34.     match line with
  35.     |element::lineTail ->
  36.         match element with
  37.         |Space(k)::tl ->
  38.             printf " lol"
  39.             matchingLine lineTail
  40.         |Word(k)::tl ->
  41.             printf " word"
  42.             matchingLine lineTail
  43.         |_ -> ()
  44.  
  45.     |_-> ()
  46.  
  47. let rec printLines linesList =
  48.     match linesList with
  49.     |line::tl ->
  50.         matchingLine line
  51.         printfn "|"
  52.         printLines tl
  53.     |_ -> ()
  54.  
  55. printLines abc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement