-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
377 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Instructions | ||
|
||
Implement basic list operations. | ||
|
||
In functional languages list operations like `length`, `map`, and `reduce` are very common. | ||
Implement a series of basic list operations, without using existing functions. | ||
|
||
The precise number and names of the operations to be implemented will be track dependent to avoid conflicts with existing names, but the general operations you will implement include: | ||
|
||
- `append` (_given two lists, add all items in the second list to the end of the first list_); | ||
- `concatenate` (_given a series of lists, combine all items in all lists into one flattened list_); | ||
- `filter` (_given a predicate and a list, return the list of all items for which `predicate(item)` is True_); | ||
- `length` (_given a list, return the total number of items within it_); | ||
- `map` (_given a function and a list, return the list of the results of applying `function(item)` on all items_); | ||
- `foldl` (_given a function, a list, and initial accumulator, fold (reduce) each item into the accumulator from the left_); | ||
- `foldr` (_given a function, a list, and an initial accumulator, fold (reduce) each item into the accumulator from the right_); | ||
- `reverse` (_given a list, return a list with all the original items, but in reversed order_). | ||
|
||
Note, the ordering in which arguments are passed to the fold functions (`foldl`, `foldr`) is significant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/list-ops.lfe" | ||
], | ||
"test": [ | ||
"test/list-ops-tests.lfe" | ||
], | ||
"example": [ | ||
".meta/example.lfe" | ||
] | ||
}, | ||
"blurb": "Implement basic list operations." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(defmodule list-ops | ||
(export | ||
(append 2) | ||
(concat 1) | ||
(filter 2) | ||
(length 1) | ||
(map 2) | ||
(foldl 3) | ||
(foldr 3) | ||
(reverse 1) | ||
) | ||
) | ||
|
||
(defun append (list1 list2) (append-loop (reverse list1) list2)) | ||
(defun append-loop | ||
((list1 []) (reverse list1)) | ||
((list1 (cons next tail)) (append-loop (cons next list1) tail))) | ||
|
||
(defun concat (content) (concat-loop content '())) | ||
(defun concat-loop | ||
(('() acc) acc) | ||
(((cons next tail) acc) (concat-loop tail (append acc next)))) | ||
|
||
(defun filter (content filter-func) | ||
(filter content '() filter-func) | ||
) | ||
|
||
(defun filter | ||
(('() acc filter-func) (lists:reverse acc)) | ||
(((cons next tail) acc filter-func) | ||
(if (funcall filter-func next) | ||
(filter tail (cons next acc) filter-func) | ||
(filter tail acc filter-func) | ||
) | ||
) | ||
) | ||
|
||
(defun length (contents) (foldl-loop contents 0 (lambda (acc item) (+ acc 1)))) | ||
|
||
(defun map (contents map-func) | ||
(map-loop contents '() map-func) | ||
) | ||
(defun map-loop | ||
(('() acc map-func) (reverse acc)) | ||
(((cons next tail) acc map-func) (map-loop tail (cons (funcall map-func next) acc) map-func)) | ||
) | ||
|
||
(defun foldl (contents initial fold-func) (foldl-loop contents initial fold-func)) | ||
|
||
(defun foldr (contents initial fold-func) (foldl-loop (reverse contents) initial fold-func)) | ||
|
||
(defun foldl-loop | ||
(('() acc fold-func) acc) | ||
(((cons head tail) acc fold-func) (foldl-loop tail (funcall fold-func acc head) fold-func)) | ||
) | ||
|
||
(defun reverse (contents) (reverse-loop contents '())) | ||
(defun reverse-loop | ||
(('() reversed) reversed) | ||
(((cons next tail) acc) (reverse-loop tail (cons next acc))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[485b9452-bf94-40f7-a3db-c3cf4850066a] | ||
description = "append entries to a list and return the new list -> empty lists" | ||
|
||
[2c894696-b609-4569-b149-8672134d340a] | ||
description = "append entries to a list and return the new list -> list to empty list" | ||
|
||
[e842efed-3bf6-4295-b371-4d67a4fdf19c] | ||
description = "append entries to a list and return the new list -> empty list to list" | ||
|
||
[71dcf5eb-73ae-4a0e-b744-a52ee387922f] | ||
description = "append entries to a list and return the new list -> non-empty lists" | ||
|
||
[28444355-201b-4af2-a2f6-5550227bde21] | ||
description = "concatenate a list of lists -> empty list" | ||
|
||
[331451c1-9573-42a1-9869-2d06e3b389a9] | ||
description = "concatenate a list of lists -> list of lists" | ||
|
||
[d6ecd72c-197f-40c3-89a4-aa1f45827e09] | ||
description = "concatenate a list of lists -> list of nested lists" | ||
|
||
[0524fba8-3e0f-4531-ad2b-f7a43da86a16] | ||
description = "filter list returning only values that satisfy the filter function -> empty list" | ||
|
||
[88494bd5-f520-4edb-8631-88e415b62d24] | ||
description = "filter list returning only values that satisfy the filter function -> non-empty list" | ||
|
||
[1cf0b92d-8d96-41d5-9c21-7b3c37cb6aad] | ||
description = "returns the length of a list -> empty list" | ||
|
||
[d7b8d2d9-2d16-44c4-9a19-6e5f237cb71e] | ||
description = "returns the length of a list -> non-empty list" | ||
|
||
[c0bc8962-30e2-4bec-9ae4-668b8ecd75aa] | ||
description = "return a list of elements whose values equal the list value transformed by the mapping function -> empty list" | ||
|
||
[11e71a95-e78b-4909-b8e4-60cdcaec0e91] | ||
description = "return a list of elements whose values equal the list value transformed by the mapping function -> non-empty list" | ||
|
||
[613b20b7-1873-4070-a3a6-70ae5f50d7cc] | ||
description = "folds (reduces) the given list from the left with a function -> empty list" | ||
include = false | ||
|
||
[e56df3eb-9405-416a-b13a-aabb4c3b5194] | ||
description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list" | ||
include = false | ||
|
||
[d2cf5644-aee1-4dfc-9b88-06896676fe27] | ||
description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list" | ||
include = false | ||
|
||
[36549237-f765-4a4c-bfd9-5d3a8f7b07d2] | ||
description = "folds (reduces) the given list from the left with a function -> empty list" | ||
reimplements = "613b20b7-1873-4070-a3a6-70ae5f50d7cc" | ||
|
||
[7a626a3c-03ec-42bc-9840-53f280e13067] | ||
description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list" | ||
reimplements = "e56df3eb-9405-416a-b13a-aabb4c3b5194" | ||
|
||
[d7fcad99-e88e-40e1-a539-4c519681f390] | ||
description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list" | ||
reimplements = "d2cf5644-aee1-4dfc-9b88-06896676fe27" | ||
|
||
[aeb576b9-118e-4a57-a451-db49fac20fdc] | ||
description = "folds (reduces) the given list from the right with a function -> empty list" | ||
include = false | ||
|
||
[c4b64e58-313e-4c47-9c68-7764964efb8e] | ||
description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list" | ||
include = false | ||
|
||
[be396a53-c074-4db3-8dd6-f7ed003cce7c] | ||
description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list" | ||
include = false | ||
|
||
[17214edb-20ba-42fc-bda8-000a5ab525b0] | ||
description = "folds (reduces) the given list from the right with a function -> empty list" | ||
reimplements = "aeb576b9-118e-4a57-a451-db49fac20fdc" | ||
|
||
[e1c64db7-9253-4a3d-a7c4-5273b9e2a1bd] | ||
description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list" | ||
reimplements = "c4b64e58-313e-4c47-9c68-7764964efb8e" | ||
|
||
[8066003b-f2ff-437e-9103-66e6df474844] | ||
description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list" | ||
reimplements = "be396a53-c074-4db3-8dd6-f7ed003cce7c" | ||
|
||
[94231515-050e-4841-943d-d4488ab4ee30] | ||
description = "reverse the elements of the list -> empty list" | ||
|
||
[fcc03d1e-42e0-4712-b689-d54ad761f360] | ||
description = "reverse the elements of the list -> non-empty list" | ||
|
||
[40872990-b5b8-4cb8-9085-d91fc0d05d26] | ||
description = "reverse the elements of the list -> list of lists is not flattened" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
ERL := $(shell which erl) | ||
REBAR3 := $(shell which rebar3) | ||
|
||
null := | ||
space := $(null) # | ||
comma := , | ||
|
||
ifeq ($(ERL),) | ||
$(error Can't find Erlang executable 'erl') | ||
else ifeq ($(REBAR3),) | ||
$(error Can't find rebar3) | ||
endif | ||
|
||
compile: ; $(REBAR3) compile | ||
|
||
clean: ; $(REBAR3) clean | ||
|
||
.PHONY: test | ||
test: | ||
$(REBAR3) eunit \ | ||
-m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{plugins, [{rebar3_lfe, "0.4.3"}]}. | ||
|
||
{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. | ||
|
||
{deps, [{lfe, "2.1.1"}]}. | ||
|
||
{profiles, | ||
[{test, | ||
[{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, | ||
{deps, | ||
[{ltest, "0.13.3"}]}]}]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{"1.2.0", | ||
[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}. | ||
[ | ||
{pkg_hash,[ | ||
{<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]}, | ||
{pkg_hash_ext,[ | ||
{<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]} | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
%% -*- erlang -*- | ||
{application, 'list-ops', | ||
[{description, "Implement basic list operations."}, | ||
{vsn, "0.0.1"}, | ||
{modules,['list-ops']}, | ||
{registered, []}, | ||
{applications, | ||
[kernel, stdlib]}, | ||
{included_applications, []}, | ||
{env, []}]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(defmodule list-ops | ||
(export | ||
(append 2) | ||
(concat 1) | ||
(filter 2) | ||
(length 1) | ||
(map 2) | ||
(foldl 3) | ||
(foldr 3) | ||
(reverse 1) | ||
) | ||
) | ||
|
||
(defun append (list1 list2) | ||
; Please implement the 'append' function | ||
) | ||
|
||
(defun concat (content) | ||
; Please implement the 'concat' function | ||
) | ||
|
||
(defun filter (content filter-func) | ||
; Please implement the 'filter' function | ||
) | ||
|
||
(defun length (contents) | ||
; Please implement the 'length' function | ||
) | ||
|
||
(defun map (contents map-func) | ||
; Please implement the 'map' function | ||
) | ||
|
||
(defun foldl (contents initial fold-func) | ||
; Please implement the 'foldl' function | ||
) | ||
|
||
(defun foldr (contents initial fold-func) | ||
; Please implement the 'foldr' function | ||
) | ||
|
||
|
||
(defun reverse (contents) | ||
; Please implement the 'reverse' function | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
(defmodule list-ops-tests | ||
(behaviour ltest-unit) | ||
(export all)) | ||
|
||
(include-lib "ltest/include/ltest-macros.lfe") | ||
|
||
(deftest append-empty-lists | ||
(is-equal '() (list-ops:append '() '()))) | ||
|
||
(deftest append-list-to-empty-list | ||
(is-equal '(1 2 3 4) (list-ops:append '() '(1 2 3 4)))) | ||
|
||
(deftest append-empty-list-to-list | ||
(is-equal '(1 2 3 4) (list-ops:append '(1 2 3 4) '()))) | ||
|
||
(deftest append-non-empty-lists | ||
(is-equal '(1 2 2 3 4 5) (list-ops:append '(1 2) '(2 3 4 5)))) | ||
|
||
(deftest concat-empty-list | ||
(is-equal '() (list-ops:concat '()))) | ||
|
||
(deftest concat-list-of-lists | ||
(is-equal '(1 2 3 4 5 6) (list-ops:concat (list (list 1 2) (list 3) '() (list 4 5 6))))) | ||
|
||
(deftest concat-list-of-nested-lists | ||
(is-equal (list (list 1) (list 2) (list 3) '() (list 4 5 6)) (list-ops:concat (list (list (list 1) (list 2)) (list (list 3)) (list '()) (list (list 4 5 6)))))) | ||
|
||
(deftest filter-empty-list | ||
(is-equal '() (list-ops:filter '() (lambda (x) (== (rem x 2) 1))))) | ||
|
||
(deftest filter-non-empty-list | ||
(is-equal '(1 3 5) (list-ops:filter '(1 2 3 5) (lambda (x) (== (rem x 2) 1))))) | ||
|
||
(deftest length-empty-list | ||
(is-equal 0 (list-ops:length '()))) | ||
|
||
(deftest length-non-empty-list | ||
(is-equal 4 (list-ops:length `(1 2 3 4)))) | ||
|
||
(deftest map-empty-list | ||
(is-equal '() (list-ops:map '() (lambda (x) (+ x 1))))) | ||
|
||
(deftest map-non-empty-list | ||
(is-equal '(2 4 6 8) (list-ops:map '(1 3 5 7) (lambda (x) (+ x 1))))) | ||
|
||
(deftest foldl-empty-list | ||
(is-equal 2 (list-ops:foldl '() 2 (lambda (acc el) (* acc el))))) | ||
|
||
(deftest foldl-direction-independent-function-applied-to-non-empty-list | ||
(is-equal 15 (list-ops:foldl '(1 2 3 4) 5 (lambda (acc el) (+ acc el))))) | ||
|
||
(deftest foldl-direction-dependent-function-applied-to-non-empty-list | ||
(is-equal 64.0 (list-ops:foldl '(1 2 3 4) 24 (lambda (acc el) (/ el acc))))) | ||
|
||
(deftest foldr-empty-list | ||
(is-equal 2 (list-ops:foldr '() 2 (lambda (acc el) (* el acc))))) | ||
|
||
(deftest foldr-direction-independent-function-applied-to-non-empty-list | ||
(is-equal 15 (list-ops:foldr '(1 2 3 4) 5 (lambda (acc el) (+ el acc))))) | ||
|
||
(deftest foldr-direction-dependent-function-applied-to-non-empty-list | ||
(is-equal 9.0 (list-ops:foldr '(1 2 3 4) 24 (lambda (acc el) (/ el acc))))) | ||
|
||
(deftest reverse-empty-list | ||
(is-equal '() (list-ops:reverse '()))) | ||
|
||
(deftest reverse-non-empty-list | ||
(is-equal '(7 5 3 1) (list-ops:reverse '(1 3 5 7)))) | ||
|
||
(deftest reverse-list-of-lists-is-not-flattened | ||
(is-equal (list '(4 5 6) '() '(3) '(1 2)) (list-ops:reverse (list '(1 2) '(3) '() '(4 5 6))))) |