Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some list functions. #182

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

puripuri2100
Copy link
Contributor

functions

  • head : 'a list -> 'a option
  • tail : 'a list -> ('a list) option
  • reverse-append : 'a list -> 'a list -> 'a list
  • reverse-map : ('a -> 'b) -> 'a list -> 'b list
  • for-all : ('a -> bool) -> 'a list -> bool
  • exists : ('a -> bool) -> 'a list -> bool
  • for-all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option
  • exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool option
  • find : ('a -> bool) -> 'a list -> 'a option
  • partition : ('a -> bool) -> 'a list -> 'a list * 'a list
  • split : ('a * 'b) list -> 'a list * 'b list
  • combine : 'a list -> 'b list -> (('a * 'b) list) option

reference

@puripuri2100
Copy link
Contributor Author

  • last : 'a list -> 'a option
    last [] %==> None
    last [1] %==> Some(1)
    last [1;2;3;4] %==> Some(4)
    
  • init : 'a list -> ('a list) option
    init [] %==> None
    init [1] %==> Some([])
    init [1;2;3;4] %==> Some([1;2;3])
    
  • take : int -> 'a list -> 'a list
    take 2 [] %==> []
    take 2 [1] %==> [1]
    take 2 [1;2;3;4] %==> [1;2;3]
    take 0 [] %==> []
    take 0 [1] %==> [1]
    take 0 [1;2;3;4] %==> [1]
    take (-1) [] %==> []
    take (-1) [1] %==> []
    take (-1) [1;2;3;4] %==> []
    
  • drop : int -> 'a list -> 'a list
    drop 2 [] %==> []
    drop 2 [1] %==> []
    drop 2 [1;2;3;4] %==> [4]
    drop 0 [] %==> []
    drop 0 [1] %==> []
    drop 0 [1;2;3;4] %==> [2;3;4]
    drop (-1) [] %==> []
    drop (-1) [1] %==> [1]
    drop (-1) [1;2;3;4] %==> [1;2;3;4]
    
  • bubblesort : ('a -> 'a -> int) -> 'a list -> 'a list
    let f n m =
      if n < m then
          -1
        else
          if n > m then
            1
          else
            0
    in
    
    bubblesort f [] %==> []
    bubblesort f [1] %==> [1]
    bubblesort f [1;2;3;4] %==> [1;2;3;4]
    bubblesort f [4;3;2;1] %==> [1;2;3;4]
    bubblesort f [2;4;3;1] %==> [1;2;3;4]
    bubblesort f [1;2;3;4;3;3] %==> [1;2;3;3;3;4]
    

@puripuri2100
Copy link
Contributor Author

  • apply : 'a -> ('a -> 'b) list -> 'b list
    let f2 n m = n + m in
    apply 5 [f2 1; f2 2; f2 3] % ==>[6; 7; 8]
    
  • show-opt : ('a option) list -> 'a list
    show-opt [Some(1); Some(2); None; Some(4);] %==>[1;2;4]
    show-opt [None; None;] %==> []
    
  • iterate : int -> ('a -> 'a) -> 'a -> 'a list
    iterate 5 (fun n -> n + 1) 2 %==>[3; 4; 5; 6; 7]
    iterate 0 (fun n -> n + 1) 2 %==> []
    
  • repeat : int -> 'a -> 'a list
    repeat 5 2 %==>[2;2;2;2;2]
    
  • make-cycle : int -> 'a list -> 'a list
    2 [1;3;2] %==> [1;3;2;1;3;2]
    
  • takewhile : ('a -> bool) -> 'a list -> 'a list
    takewhile (fun x -> x < 4) [1;2;3;4;5] %==> [1; 2; 3]
    
  • dropwhile : ('a -> bool) -> 'a list -> 'a list
    dropwhile (fun x -> x < 4) [1;2;3;4;5] %==> [4;5]
    
  • splitat : int -> 'a list -> ('a list * 'a list)
    splitat 2 [1;2;3;4;5] %==>([1; 2; 3], [4; 5])
    
  • span : ('a -> bool) -> 'a list -> ('a list * 'a list)
    span (fun x -> x < 3) [1;2;3;4;5] %==>([1;2], [3;4;5])
    
  • break : ('a -> bool) -> 'a list -> ('a list * 'a list)
    break (fun x -> x > 3) [1;2;3;4;5] %==> ([1;2;3], [4;5])
    

@puripuri2100
Copy link
Contributor Author

That's all.
I would appreciate it if you could merge this PR. @gfngfn

@nyuichi
Copy link
Contributor

nyuichi commented Jul 13, 2019

@puripuri2100

Hi, I love this patch. I'd like to use your functions in my personal project soon, and can I (or perhaps can you) import this also to list2.satyh at satysfi-lib?

@puripuri2100
Copy link
Contributor Author

This change has already imported in satysfi-base.
Should I close this pull request? 🤔
@gfngfn

@gfngfn gfngfn added this to the v0.0.12 milestone Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants