Skip to content

Instantly share code, notes, and snippets.

@jarble
Created September 27, 2021 18:57
Show Gist options
  • Save jarble/e5a1c85ffa76b86981c8eeb29ac40b01 to your computer and use it in GitHub Desktop.
Save jarble/e5a1c85ffa76b86981c8eeb29ac40b01 to your computer and use it in GitHub Desktop.
Replacing matching sublists in Erlang
-module(helloworld).
-import(lists,[append/2]).
-export([start/0]).
replace_if_match(X) ->
case X of
[A, "equals", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
[A, "is", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
[A, "is","equal","to", B | Tail ] ->
[[A,"==",B],replace_if_match(Tail)];
[Head | Tail] ->
[Head|replace_if_match(Tail)];
[] -> []
end.
start() ->
Lst1 = ["A", "is", ["1","+","1"], "and", "B","equals",["2","+","A"]],
io:fwrite(replace_if_match(Lst1)). % prints "A==1+1andB==2+A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment