# How to translate this Erlang code to LFE?

**URL:** <https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752>\
**Category:** LFE Questions\
**Tags:** lfe, lfe-questions\
**Created:** [December 7, 2021, 12:43am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752 "2021-12-07T00:43:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kokolegorille](https://erlangforums.com/user_avatar/erlangforums.com/kokolegorille/32/223_2.png) [@kokolegorille](https://erlangforums.com/u/kokolegorille)\
**Post date:** [December 7, 2021, 12:43am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/1 "2021-12-07T00:43:10Z")

</div>

Hello everyone,

I have been trying to solve AOC with LFE, yet there is something I don’t understand.

This is the Erlang code…

```erlang
-module(day6).
-export([load_data/0]).

load_data() ->
    {ok, Bin} = file:read_file("input.txt"),
    List = binary:split(string:trim(Bin), <<",">>, [global, trim]),
    lists:map(fun(X) -> 
        {V, _} = string:to_integer(X), 
        V 
    end, List).

```

and this almost the same in LFE

```erlang
(defmodule day6 (export (load_data 0)))

(defun load_data ()
    (let 
        (((tuple 'ok bin) (file:read_file "input.txt"))) 
        (lists:map 
            (lambda (x) (string:to_integer x))
            (string:split (string:trim bin) "," 'all))))

```

I cannot figure how to replace this

```erlang
    fun(X) -> 
        {V, _} = string:to_integer(X), 
        V 
    end

```

and this would be the LFE code.

```erlang
(lambda (x) (string:to_integer x))

```

but string:to\_integer x returns a tuple, and I am only interested in the first value.

How can I return only the first element?

Thanks for taking time.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://erlangforums.com/user_avatar/erlangforums.com/kokolegorille/32/223_2.png) [@kokolegorille](https://erlangforums.com/u/kokolegorille)\
**Post date:** [December 7, 2021, 12:59am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/2 "2021-12-07T00:59:37Z")

</div>

Sorry for the question… I just found the solution after posting.

```erlang
(defun load_data ()
    (let 
        (((tuple 'ok bin) (file:read_file "input.txt"))) 
        (lists:map 
            (lambda (x) (let (((tuple y _) (string:to_integer x))) y))
            (string:split (string:trim bin) "," 'all))))

```

It is working as expected. I did not fully understood the let macro 🙂

---

<div class="post-metadata">

**Author:** ![cmkarlsson](https://erlangforums.com/user_avatar/erlangforums.com/cmkarlsson/32/416_2.png) [@cmkarlsson](https://erlangforums.com/u/cmkarlsson)\
**Post date:** [December 7, 2021, 1:04am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/3 "2021-12-07T01:04:29Z")

</div>

My lfe skills are not great but can’t you just let bind the value, just as you do for your `file:read_file`?

```erlang
(lambda (x) 
  (let (((tuple v _) (string:to_integer x)))
  v))

```

Or you could use `element/2`

```erlang
(lambda (x) (element 1 (string:to_integer x)))

```

EDIT: Oops, late to the party 😃

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://erlangforums.com/user_avatar/erlangforums.com/kokolegorille/32/223_2.png) [@kokolegorille](https://erlangforums.com/u/kokolegorille)\
**Post date:** [December 7, 2021, 1:07am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/4 "2021-12-07T01:07:13Z")

</div>

Thank You for your reply, I figure it out also, I have to say it’s my first Lisp code 🙂

---

<div class="post-metadata">

**Author:** ![rvirding](https://erlangforums.com/user_avatar/erlangforums.com/rvirding/32/178_2.png) [@rvirding](https://erlangforums.com/u/rvirding)\
**Post date:** [December 8, 2021, 11:10pm UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/5 "2021-12-08T23:10:33Z")

</div>

All I can say here is that the solutions are correct.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://erlangforums.com/user_avatar/erlangforums.com/astonj/32/9_2.png) [@AstonJ](https://erlangforums.com/u/AstonJ)\
**Post date:** [December 9, 2021, 3:11am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/6 "2021-12-09T03:11:28Z")

</div>

> [@kokolegorille](#):
>
> I have to say it’s my first Lisp code 🙂

Congrats Koko! :023:

I hope to learn a Lisp one day… and I think LFE would be perfect 😃 if we see a PragProg [book from Duncan](https://erlangforums.com/t/lisp-flavoured-erlang-a-chat-with-robert/414/14) (or Robert) I will add it further up my list :003:

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://erlangforums.com/user_avatar/erlangforums.com/kokolegorille/32/223_2.png) [@kokolegorille](https://erlangforums.com/u/kokolegorille)\
**Post date:** [December 9, 2021, 9:37am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/7 "2021-12-09T09:37:31Z")

</div>

Since then I made an AOC problem with LFE.

> [@Advent of Code 2021 - Day 7](https://erlangforums.com/t/advent-of-code-2021-day-7/754/14):
>
> My first AOC problem with LFE. (defmodule day7 (export (part1 0) (part2 0))) (defun part1 () (lists:min (lists:map (lambda (x) (fuel\_consumption x (load\_data))) (min\_max (load\_data))))) (defun part2 () (lists:min (lists:map (lambda (x) (fuel\_consumption2 x (load\_data))) (min\_max (load\_data))))) (defun min\_max (list) (lists:seq (lists:min list) (lists:max list))) (defun fuel\_consumption (pos list) (lis…

I have read the books from @oubiwann and followed the start of this course.

> **[Structure and Interpretation of Computer Programs | Electrical Engineering...](https://ocw.mit.edu/courses/6-001-structure-and-interpretation-of-computer-programs-spring-2005/)**
>
> This course introduces students to the principles of computation. Upon completion of 6.001, students should be able to explain and apply the basic methods from programming languages to analyze computational systems, and to generate computational...

Here is the secret I have learned 🙂

```erlang
# Elixir
input |> ... |> ... |>

%% Erlang
S0=...(Input).
S1=...(S0).
S3=...(S1).

; LFE
(...(...(...input)))

```

---

<div class="post-metadata">

**Author:** ![rvirding](https://erlangforums.com/user_avatar/erlangforums.com/rvirding/32/178_2.png) [@rvirding](https://erlangforums.com/u/rvirding)\
**Post date:** [January 1, 2022, 2:14am UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/8 "2022-01-01T02:14:08Z")

</div>

@oubiwann has written a lot of great stuff for/about/using LFE.

---

<div class="post-metadata">

**Author:** ![mdbergmann](https://erlangforums.com/user_avatar/erlangforums.com/mdbergmann/32/1243_2.png) [@mdbergmann](https://erlangforums.com/u/mdbergmann)\
**Post date:** [February 5, 2022, 11:08pm UTC](https://erlangforums.com/t/how-to-translate-this-erlang-code-to-lfe/752/9 "2022-02-05T23:08:58Z")

</div>

There is also Clojures threading:

```erlang
(-> input
  (s0)
  (s1)
  (s2)

```

Clojure macros are available in LFE as import.
