Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.60 KB | None | 0 0
  1. open System
  2.  
  3. type LineElement =
  4.     |Word of string
  5.     |Space of int
  6.  
  7. type NewLineElement = Line of (LineElement list)*int*int
  8.  
  9. let nspace n =
  10.     let rec nspace' n str =
  11.        match n with
  12.        |0->str
  13.        |_->nspace' (n-1) (str+" ")
  14.     nspace' n ""
  15.  
  16. let list = [Word("jgxzczx") ; Word ("svyaza"); Word ("korova"); Word ("at"); Word("let"); Word("kok"); Word("s"); Word("ere"); Word("oblom"); Word("Kirovreporting")]                
  17.  
  18. let divide list num =
  19.    let rec divide' list num acc list' list'' numwd =
  20.         match list with
  21.            |hd::tl ->
  22.                match hd with
  23.                    |Word a when (acc+String.length a<=num) -> divide' tl num (acc+String.length a+1) (list') (list''@[Space(1)]@[Word(a)]) (numwd+1)
  24.                    |Word a when (acc+String.length a>num)-> divide' tl num (String.length a) (list'@ [Line(list'', acc, numwd+1)]) ([Word(a)]) (0)
  25.            |[]->list'@[Line(list'',acc, numwd+1)]
  26.     divide' list num 0 [] [] 0
  27.  
  28. let rec alignleft list num =
  29.    match list with
  30.        |hd::tl->
  31.            match hd with
  32.                |Line(list',a, numwd)->[Line(list'@[Space(num-a)], a, numwd)] @ alignleft tl num
  33.        |[]->[]
  34.  
  35. let rec aligncentre list num =
  36.    match list with
  37.        |hd::tl->
  38.            match hd with
  39.                |Line(list',a, numwd)->[Line([Space((num-a)%2 + (num-a)/2)]@list'@[Space((num-a)/2)], a, numwd)] @ aligncentre tl num
  40.        |[]->[]
  41.  
  42. let rec alignright list num =
  43.    match list with
  44.        |hd::tl->
  45.            match hd with
  46.                |Line(list',a, numwd)->[Line([Space(num-a)]@list', a, numwd)] @ alignright tl num
  47.        |[]->[]
  48.  
  49. let newline list numwd numlet num =
  50.    let rec newline' list numwd numlet num listnak=
  51.        match list with
  52.        |(Space k)::tl-> newline' tl numwd numlet num (Space ((num-numlet)/numwd)::listnak)
  53.       |(Word k)::tl -> newline' tl numwd numlet num (Word k::listnak)
  54.        |[]->listnak
  55.     Line(newline' list numwd numlet num [], numlet, numwd)
  56.  
  57.  
  58. let rec alignwidth list num =
  59.      match list with
  60.        |hd::tl->
  61.            match hd with
  62.            |Line(list',a, numwd)-> (newline list' numwd a num)::(alignwidth tl num)
  63.        |[]->[]
  64.  
  65.  
  66. let rec printline line=
  67.    match line with
  68.        |Space(k)::tl->printf "%A" (nspace k); printline tl
  69.        |Word(k)::tl->printf "%A" k; printline tl
  70.        |[]->printfn ""
  71.  
  72. let rec printtext list =
  73.    match list with
  74.        Line(list', a, b)::tl->printline list'; printtext tl
  75.        |[]->printfn  "";[1]
  76.  
  77. let sc = divide list 20
  78.  
  79. let m = alignwidth sc 20
  80.  
  81. let r = printtext m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement