[personal profile] gdsfh
komar справедливо заметил некоторое различие в способах вызова окамловских List.fold_{left,right}. Мне тоже это не нравится. Попробуем это решить с максимальным количеством ональных извращений. Ну, чисто на всякий случай. Проверить свою силу, так сказать. Конечно, я -- сугубо против такого кода, сообщу сразу, в качестве дисклизмера. Несмотря на это, буду лелеять своего внутреннего индуса на специально отведённой территории.

$ ocaml
        Objective Caml version 3.12.1+rc1

# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
# #camlp4r;;
/home/gds/dev/ovm/ocaml/lib/dynlink.cma: loaded
/home/gds/dev/ovm/ocaml/lib/camlp4: added to search path
/home/gds/dev/ovm/ocaml/lib/camlp4/camlp4r.cma: loaded
	Camlp4 Parsing version 3.12.1+rc1

# open List;

# fold_left;
- : ('a -> 'b -> 'a) -> 'a -> list 'b -> 'a = <fun>

# fold_right;
- : ('a -> 'b -> 'b) -> list 'a -> 'b -> 'b = <fun>

# value flip_2_3_of_3 f = fun x y z -> f x z y;
value flip_2_3_of_3 : ('a -> 'b -> 'c -> 'd)
  -> 'a -> 'c -> 'b -> 'd = <fun>

# value map_1_of_3 m f = fun x y z -> f (m x) y z;
value map_1_of_3 :
  ('a -> 'b) -> ('b -> 'c -> 'd -> 'e)
  -> 'a -> 'c -> 'd -> 'e = <fun>

# value ( |> ) x f = f x;
value ( |> ) : 'a -> ('a -> 'b) -> 'b = <fun>

# value flip_1_2_of_2 f = fun x y -> f y x;
value flip_1_2_of_2 : ('a -> 'b -> 'c)
  -> 'b -> 'a -> 'c = <fun>

# value r2l fold_right =
  fold_right
  |> flip_2_3_of_3
  |> (map_1_of_3 flip_1_2_of_2);
value r2l :
  (('a -> 'b -> 'c) -> 'd -> 'e -> 'f)
  -> ('b -> 'a -> 'c) -> 'e -> 'd -> 'f =
  <fun>

# value do_with_foldl foldl =
    Printf.printf "res=%s\n%!"
      (foldl
         (fun acc s -> acc ^ s)
         ">"
         ["a";"b";"c"]
      );
value do_with_foldl :
  ((string -> string -> string)
    -> string -> list string -> string)
  -> unit =
  <fun>


# do_with_foldl fold_left;
res=>abc
- : unit = ()

# do_with_foldl (r2l fold_right);
res=>cba
- : unit = ()

# 
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

Profile

gdsfh

August 2013

S M T W T F S
    123
45678910
111213 14151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 11th, 2025 11:03 am
Powered by Dreamwidth Studios