Skip to content

Commit

Permalink
Update src.ml
Browse files Browse the repository at this point in the history
  • Loading branch information
sdw0316 authored Nov 9, 2023
1 parent 934a9f0 commit 14ab758
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions OCaml/diff-35/buggy/src.ml
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
type aexp =
| Const of int
| Var of string
| Power of (string * int)
| Power of string * int
| Times of aexp list
| Sum of aexp list

let rec map (f : 'c * 'b -> 'a) (l : 'c list) (x : string) : 'a list =
match l with [] -> [] | hd :: tl -> f (hd, x) :: map f tl x
let rec map f l x=
match l with
| [] -> []
| hd::tl -> (f (hd, x))::(map f tl x)


let rec diff ((aexp : aexp), (x : string)) : aexp =
match aexp with
| Const n -> Const 0
| Var s -> if s = x then Const 1 else Const 0
| Power (s, n) ->
if s = x then Times ([ Const n ] @ [ Power (s, n - 1) ]) else Const 0
| Times [] -> Times []
| [ Times hd; tl ] ->
Sum
( [ Times ([ diff (hd, x) ] @ [ tl ]) ]
@ [ Times ([ hd ] @ [ diff (tl, x) ]) ] )
| Times hd :: tl ->
Sum
( [ Times ([ diff (hd, x) ] @ tl) ]
@ [ Times ([ hd ] @ [ diff (Times tl, x) ]) ] )
| Sum lst -> Sum (map diff lst x)
let rec diff : aexp * string -> aexp
=fun (aexp,x) -> match aexp with
Const n -> Const 0
|Var s -> if (s = x) then Const 1 else Const 0
|Power (s, n) -> if (s = x) then Times ([Const n] @ [Power (s, n-1)]) else Const 0
|Times [] -> Times []
|Times (hd::tl::[]) -> Sum ([Times ([diff (hd, x)] @ [tl])] @ [Times ([hd] @ [diff (tl, x)])])
|Times (hd::tl) -> Sum ([Times ([diff (hd, x)] @ tl)] @ [Times ([hd] @ [diff (Times tl, x)])])
|Sum lst -> Sum (map diff lst x)

0 comments on commit 14ab758

Please sign in to comment.