Difference between logic programming and functional programming

前端 未结 8 905
萌比男神i
萌比男神i 2021-01-29 22:56

I have been reading many articles trying to understand the difference between functional and logic programming, but the only deduction I have been able to make so far is that lo

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 23:40

    Logic programming and functional programming use different "metaphors" for computation. This often affects how you think about producing a solution, and sometimes means that different algorithms come naturally to a functional programmer than a logic programmer.

    Both are based on mathematical foundations that provide more benefits for "pure" code; code that doesn't operate with side effects. There are languages for both paradigms that enforce purity, as well as languages that allow unconstrained side effects, but culturally the programmers for such languages tend to still value purity.

    I'm going to consider append, a fairly basic operation in both logical and functional programming, for appending a list on to the end of another list.

    In functional programming, we might consider append to be something like this:

    append [] ys = ys
    append (x:xs) ys = x : append xs ys
    

    While in logic programming, we might consider append to be something like this:

    append([], Ys, Ys).
    append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).
    

    These implement the same algorithm, and even work basically the same way, but they "mean" something very different.

    The functional append defines the list that results from appending ys onto the end of xs. We think of append as a function from two lists to another list, and the runtime system is designed to calculate the result of the function when we invoke it on two lists.

    The logical append defines a relationship between three lists, which is true if the third list is the elements of the first list followed by the elements of the second list. We think of append as a predicate that is either true or false for any 3 given lists, and the runtime system is designed to find values that will make this predicate true when we invoke it with some arguments bound to specific lists and some left unbound.

    The thing that makes logical append different is you can use it to compute the list that results from appending one list onto another, but you can also use it to compute the list you'd need to append onto the end of another to get a third list (or whether no such list exists), or to compute the list to which you need to append another to get a third list, or to give you two possible lists that can be appended together to get a given third (and to explore all possible ways of doing this).

    While equivalent in that you can do anything you can do in one in the other, they lead to different ways of thinking about your programming task. To implement something in functional programming, you think about how to produce your result from the results of other function calls (which you may also have to implement). To implement something in logic programming, you think about what relationships between your arguments (some of which are input and some of which are output, and not necessarily the same ones from call to call) will imply the desired relationship.

提交回复
热议问题